How do I pass arguments into an eventListener in JavaScript efficiently? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do I pass arguments into an eventListener in JavaScript efficiently?

Here is the problem: I want to assign just a single eventListener to 3 different div elements and pass in a function into the eventListener that executes differently based on the id of each div. The problem is that the program tells me instead that it can't read the id attribute of 'undefined'. Here is the code: https://code.sololearn.com/W0Jn3lLeL5L0/?ref=app

13th Nov 2018, 5:18 PM
Neo-Constel
Neo-Constel - avatar
2 Answers
+ 3
modified your code. you have what you need now https://code.sololearn.com/WT8klzzt9vL3/?ref=app
13th Nov 2018, 6:05 PM
᠌᠌Brains[Abidemi]
᠌᠌Brains[Abidemi] - avatar
0
᠌᠌brains Thanks a lot! I also found out just now through a random guess that replacing alert(divs[i].id); with alert(this.id); gave the same effect. I Don't know what the difference is, but I'm so happy! ☺️
13th Nov 2018, 6:13 PM
Neo-Constel
Neo-Constel - avatar