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

100
Visualizações
Why is this event handler from a class loop not responding?

I'm looping through 10 divs with the same class to add event handlers which should change the border on a mouse click. I'm using getElementsByClassName. There is no response to the click on any of the divs. The simple function worked on another div in an event handler.

The html:

<div class="note-small">
    <p>blah</p>
</div>

I used this vanilla JS:

const note_small_class = document.getElementsByClassName('note-small');

for (let i=0; i < note_small_class.length; i++) {
  note_small_class[i].addEventListener('click', () => {
    highlightNote(note_small_class[i]);
  })
};

function highlightNote(note) {
  note.style.background.color = 'red';
};

note_small_class shows as an object (HTML collection) in the console. Something isn't working, I would appreciate any help. Thank you.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

note.style.background.color = 'red';

There seems to be an issue with what you are setting the style to be, the property should be backgroundColor, not background.color, try this:

note.style.backgroundColor = 'red';
about 4 years ago · Juan Pablo Isaza Relatório
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