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

70
Visualizações
JS DOM changing parent element class

This is not homework or test task or whatever. Please give a solution or a hint if you can. I've got 3 divs with buttons, all the divs are absolutely the same, classes are the same, I can't add ID's attributes or anything else to these divs manually. All buttons are also the same with the same conditions, no IDs etc. So I need on a click to change the class for ONLY a parent element of a clicked button. How can I get to that particular parent if all divs and buttons are the same? Thanks

My HTML:

<body>
  <div class="off">
    <button>Button</button>
  </div>
  <div class="off">
    <button>Button</button>
  </div>
  <div class="off">
    <button>Button</button>
  </div>
</body>

JS:

document.addEventListener('click' , e => {

    let buttonClicked = e.target.matches('button');

    if ( buttonClicked ) {
        console.log("Button Clicked")
     }

})
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