Skip to curriculum
Jump to the editor

16.2. Implement Element.closest

Easy
Topics

This question is in

16. DOM: finding elements
10–15 min

element.closest(selector) answers a question event handlers ask all the time: "which card, row or menu item was this click inside?" It starts at the element itself and goes up through its ancestors until one matches the selector. With one listener on a whole list, event.target.closest("li") finds the item that was clicked, whatever was clicked inside it.

Write closest(element, selector) in closest.js. It returns the same element element.closest(selector) would, without calling it.

Signature

JAVASCRIPT
function closest(element, selector) // returns Element or null
  • element: the element where the search starts.
  • selector: any CSS selector, or selector list ("form, dialog").
  • Returns the first element that matches, or null.

Requirements

  1. The search starts with element itself: if it matches, it is the answer.
  2. Otherwise each ancestor is tested in order, nearest first, and the first match is returned.
  3. When nothing on the way up matches, the result is null.
  4. The search goes up to the <html> element and stops there: the document is never returned.
  5. Selectors are matched as the browser matches them, including combinators that look above the element ("main p"), attributes, pseudo-classes and selector lists.
  6. A tree that is not in the document is searched up to its own top element.
  7. Inside a shadow tree, the search stops at the top of that tree, as the built-in does: it never reaches the host element.
  8. An invalid selector throws a SyntaxError, even when the element has no parent.
  9. It works for very deep trees, and when Element.prototype.closest does not exist.

Examples

JAVASCRIPT
// <ul class="menu">
//   <li class="item" data-id="2"><a href="/docs"><b>Docs</b></a></li>
// </ul>
const bold = document.querySelector("b");
closest(bold, "li");          // the <li class="item">
closest(bold, "b");           // bold itself
closest(bold, ".menu > li");  // the <li>
closest(bold, "table");       // null
JAVASCRIPT
const card = document.createElement("article");
card.className = "card";
card.innerHTML = "<p><span>x</span></p>";
closest(card.querySelector("span"), ".card"); // card, though it is not in the page
closest(card.querySelector("span"), "body");  // null
JAVASCRIPT
closest(document.body, "p[[");  // throws a SyntaxError (a DOMException)

Environment

  • Plain JavaScript in a browser page; the tests call the global closest from closest.js.
  • Element.prototype.closest is not allowed: one test removes it while it runs. matches and the parent properties are allowed.
  • Selectors that use :scope are out of scope.

Hints

  1. Which property gives an element's parent, but only when that parent is also an element?
  2. Which method tells you whether one element matches a selector?

The explanation comes with the course

A walkthrough of every line of a solution, the mistakes the tests catch, other ways to write it, and links to the lessons that teach each idea.

  • Every line explained
  • The mistakes each test catches
  • Links to the lessons behind it
Back to the question
Loading the editor…