Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

296
Views
How to access class list of items in the array returned from event.composedPath, Javascript

I have a structure like this:

<div className="content-container chat-on">
 <div className="inner container">
   <section className="some more classes>
     <div className="button">click here</div>
   </section>
 </div>
</div>

But I need the whole area to have an event listener, the div with the class of chat-on.

Because there is actually another div with the same class name that needs the same listener on it, I had been using document.getElementsByClassName('chatOn') and then using a for of loop on the returned array to add the event listener. Although this kind of worked, every so often it seems to cause a weird side effect (not necessarily relevant to the current question).

Now what I'm trying to do is something along the lines of:

document.addEventListener("click", (event) => {
   if (event.target.matches("chat-on) {
       // function goes here
   }

But the problem is, when the user clicks, target is only registering the furthest nested element, and not the one with the class name of chat-on. I've tried

if (event.composedPath().includes('chat-on')

But that is always coming up false. Is there any way I can access the class names on the parent elements of the click event target?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!