+ 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?
3 Réponses
+ 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.
+ 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..
+ 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.