Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsListening 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
231,271 PointsThis 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
Full Stack JavaScript Techdegree Student 11,638 PointsAakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 PointsBut 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