Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

element.closest(selector, container) #162

Closed
caub opened this issue Feb 10, 2016 · 4 comments
Closed

element.closest(selector, container) #162

caub opened this issue Feb 10, 2016 · 4 comments

Comments

@caub
Copy link

caub commented Feb 10, 2016

It's rare but sometimes you have a reference to a container node, and want to search for the closest element match in this container, adding a second optional parameter may be interesting (either to make sure to limit the search to a subtree, or for performance reasons)

it would just be a second parameter in this function as a stop condition of the for loop
https://code.google.com/p/chromium/codesearch#chromium/src/third_party/WebKit/Source/core/dom/SelectorQuery.cpp&sq=package:chromium&l=157&dr=C&rcl=1455121616

@hzr
Copy link
Contributor

hzr commented Feb 10, 2016

This has been filed as an issue before, but was closed due to lack of interest: https://www.w3.org/Bugs/Public/show_bug.cgi?id=26841

You can always do something like this:

const closestEle = ele.closest(".blah");
if (containerEle.contains(closestEle)) {
   ...
}

This does not have the performance benefits, but I doubt closest() being a bottleneck, ever.

I'm not really opposed to it though, personally. I use the above pattern every time I use event delegation.

@caub
Copy link
Author

caub commented Feb 11, 2016

ok thanks

@caub caub closed this as completed Feb 11, 2016
@tjcrowder
Copy link

Please consider reopening and actioning this. closest as a DOM feature is only now coming into many people's consciousness. While there may have been lack of interest in 2014, there's clear interest in 2016 (this issue) and 2017 (my finding this being puzzled by the lack of a second parameter).

The second parameter is useful in event delegation:

myTableBody.addEventListener("mouseover", function(e) {
    var el = e.target.closest("td", this);
    if (el) {
        // ...
    }
});

while contains can be used, it's less clear and less efficient (granted, the efficiency is a minor point):

myTableBody.addEventListener("mouseover", function(e) {
    var el = e.target.closest("td");
    if (this.contains(el)) {
        // ...
    }
});

It's just mentally awkward to say "See if any ancestor is an X -- oh, but wait, I only want ones that are within Y."

Prior art: jQuery's closest does indeed have this second parameter: http://api.jquery.com/closest/#closest-selector-context

Specification and development cost seems low relative to the clarity and simplicity, esp. as this is still a new thing.

@annevk
Copy link
Member

annevk commented Sep 22, 2017

There is #215 to improve event delegation. Maybe if that doesn't pan out we should reconsider this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants