title: Can I check multiple ancestors in JavaScript using Element.closest()?
body: <p>I have a verbose condition:</p>
  
  <pre><code>if (
      (e.target.parentNode.classList[0] === 'my-class') ||
      (e.target.parentNode.parentNode.classList[0] === 'my-class') ||
      (e.target.parentNode.parentNode.parentNode.classList[0] === 'my-class')
  ) {
  
  /* [... CODE HERE...] */
  
  }
  </code></pre>
  
  <p>In some contexts, I can easily imagine the condition extending to 5 lines or more.</p>
  
  <p>I am aware that I can write a recursive function which checks if the next higher-up ancestor node is not <code>&lt;body&gt;</code> and, if not, runs the same recursive function on that node.</p>
  
  <p>But given that <code>Element.closest()</code> exists:</p>
  
  <p><strong>See:</strong> <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/closest" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/API/Element/closest</a></p>
  
  <p>is there a simpler way I can write the condition, something along the lines of:</p>
  
  <pre><code>if (e.target.closest('.my-class') !== false) {
  
  /* [... CODE HERE...] */
  
  }
  </code></pre>