-
Notifications
You must be signed in to change notification settings - Fork 289
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
Comments
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 I'm not really opposed to it though, personally. I use the above pattern every time I use event delegation. |
ok thanks |
Please consider reopening and actioning this. The second parameter is useful in event delegation: myTableBody.addEventListener("mouseover", function(e) {
var el = e.target.closest("td", this);
if (el) {
// ...
}
}); while 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 Specification and development cost seems low relative to the clarity and simplicity, esp. as this is still a new thing. |
There is #215 to improve event delegation. Maybe if that doesn't pan out we should reconsider this. |
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
The text was updated successfully, but these errors were encountered: