addEventListener | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

addEventListener

I can do something like this: var el = document.querySelector('#el'); var f = function(){ alert('test') } el.onclick = f;// so i can do it with any var. How can i do it with addEventListener?

12th Nov 2018, 11:34 PM
Дмитрий Эдуардович Филиппенко
Дмитрий Эдуардович Филиппенко - avatar
5 Answers
+ 4
You can do this: el.addEventListener('click', f); addEventListener takes two parameters, first should be the event name in string and another should be the function to be called.
13th Nov 2018, 3:06 AM
Шащи Ранжан
Шащи Ранжан - avatar
+ 2
x.addEventListener(" click ", func, true );
9th Aug 2021, 2:16 PM
Owethu Sotomela
Owethu Sotomela - avatar
0
3rd Aug 2020, 9:54 AM
Azamat Ahmadov
0
Drag and drop from the options below to handle the click event and use capturing propagation. x.addEventListener(" ____ ", func,___ );
3rd Aug 2020, 9:55 AM
Azamat Ahmadov
0
x.addEventListener(" click ", func,true);
18th Nov 2020, 1:25 PM
YASH SRIVASTAVA
YASH SRIVASTAVA - avatar