'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.
Solution 1:[1]
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';
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jakub Kotrs |
