<p onclick="doSomething()">Click Me</p>
So my question is why would someone add and event listener instead of adding that event directly in the element (as the sample above)?
The reason I’m asking is because adding event listeners involves more code…
var elementName= document.getElementById('elemenstsID');
elementName.addEventListener(“click”, doSomething, false);
I guess what I don’t know understand is why would someone choose to add an event listener instead, I know it is more OOP but doesn’t the “onclick”,“onload” etc do the same thing?
Sorry if my question doesn’t make any sense.
I think code is cleaner when you separate events from html.
Thanks for your reply! Make sense.
A listener also allows you to attach additional code to an event without affecting whatever might already be attached.