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 Antworten
+ 3
modified your code. you have what you need now https://code.sololearn.com/WT8klzzt9vL3/?ref=app
13th Nov 2018, 6:05 PM
᠌᠌Code X
᠌᠌Code X - 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