fs_tigre — 2010-05-12T11:24:01-04:00 — #1
<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.
cyberalien — 2010-05-12T14:08:00-04:00 — #2
I think code is cleaner when you separate events from html.
fs_tigre — 2010-05-12T15:04:38-04:00 — #3
Thanks for your reply! Make sense.
felgall — 2010-05-12T15:29:51-04:00 — #4
A listener also allows you to attach additional code to an event without affecting whatever might already be attached.
fs_tigre — 2010-05-12T20:50:49-04:00 — #5