JavaScript JavaScript and the DOM Responding to User Interaction Listening for Events with addEventListener()

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,608 Points

Listening for Events with addEventListener()

What if I use for loop within each event handler just like-

listItems.addEventListener('mouseover', () => {
for(let i = 0 ; i< listItems.length ; i+=1){
listItems[i].textContent = listItems[i].textContent.toUpperCase();
}

listItems.addEventListener('mouseout', () => {
for(let i = 0 ; i< listItems.length ; i+=1){
listItems[i].textContent = listItems[i].textContent.toLowerCase();
}

Is it correct?If not why?

1 Answer

Steven Parker
Steven Parker
172,090 Points

This code doesn't show how "listItems" is established, but since it is being indexed and used in a loop, I assume that it represents an HTMLcollection. But a collection would not have a "addEventListener" method, only the individual elements would have that.

Also, it looks like the closing brace and parenthesis are missing from each function.

Aakash Srivastav
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,608 Points

But a collection would not have a "addEventListener" method, only the individual elements would have that.

I didn't know that . It clears all my doubt. Thanks Steven