Skip to content Skip to sidebar Skip to footer

Addeventlistener Works In Simple For Loop But Doesn't Work With For-in Loop

When I use simple for loop, addEventListener works well in for loop. But when I use for-in loop, it makes error like Uncaught TypeError: checklist[i].addEventListener is not a fun

Solution 1:

The problem is that for-in loop iterates over all enumerable properties of an array or object. So, if you log your variable in console you'll see that along with the indexes of the elements you also get other properties like length, keys, values of the array and checklist[length] or checklist[keys] are not DOM elements. So you can't add an event listener to them.


Post a Comment for "Addeventlistener Works In Simple For Loop But Doesn't Work With For-in Loop"