is the better usage of onclick/anything function in HTML elements over EventListeners in js ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

is the better usage of onclick/anything function in HTML elements over EventListeners in js ?

<button onclick="click()">Click me</button> button.addEventListener("click",click); From above what is the best and efficient way? Tell me difference between them?

30th Jan 2019, 2:51 AM
Mahesh Muttinti
3 Answers
+ 4
In general, there are two differences as below: 1. addEventListener does not overwrite existing event handlers on elements, whereas onclick overrides any existing onclick = function event handlers. 2. addEventListener does not works in older versions of Internet Explorer ( > IE 9 ) which uses attachEvent instead ( < IE 9 ), whereas onclick works all the browsers.
8th Feb 2019, 8:01 AM
Odumodu Okwudili Joshua
Odumodu Okwudili Joshua - avatar
+ 2
onclick attribute is easy to use as it only needs the attribute and the name of the function() written in the Js section. In my every code I use onClick attribute..
30th Jan 2019, 6:50 AM
Sandakan Nipunajith
Sandakan Nipunajith - avatar
+ 1
I am curious for opinions about this. I think the event listener is a newer part of JavaScript, and usually new things are recommended.
30th Jan 2019, 3:09 AM
James
James - avatar