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

105
Views
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 answers
Answer question

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 Report
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!