Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda