Re: [whatwg/dom] element.closest(selector, container) (#162)

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:

```js
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)*:

```js
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.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/162#issuecomment-331413470

Received on Friday, 22 September 2017 10:39:38 UTC