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

207
Visualizações
How ot select an element in the DOM hierarchy that has a specific class?

I have a construction made up of several nested <div> and <input> components.

When I focus on one of the inputs, I would like to change the background color of the first element in the hierarchy that has the class note.

The code below shows the general idea, but it works only for the first (parent) element above the focused-on input.

document.addEventListener('focusin', (event) => changeFocusColor(event))
const changeFocusColor = (event) => {
  // here comes the computation of the element to do ...style.BackgroundColor = ... on
  event.target.parentElement.style.backgroundColor = 'yellow'
}
<div>
  <div class="note">
    <input />first level
    <div>
      <input />second level
      <input />second level
    </div>
  </div>
  <div>hello</div>
</div>

Is there a clean way to say "go up in the DOM tree until you find an element with the class note and apply a style on it"?

The way I plan to do it is to iterate over element.path and check its classList until I find note there, and then apply style.background on that element.

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