PDA

View Full Version : Diff between property assignment and DOM attaching for events?


beetle
12-23-2002, 04:40 PM
Anyone know of any practical/usable/compatible/whatever differences between assigning events as a property vs. using the addEventListener or attachEvent methods?

Examplevar d = document.getElementById('myDiv');

// Property assignment
d.onclick = function() { alert( this.nodeName ) }

// attachEvent method
d.attachEvent( "onclick", function() { alert( this.nodeName ) } );

// addEventListener method
d.addEventListener( "click", function() { alert( this.nodeName ) }, false );Ok, I recognize that addEventListener differs in that it allows you to toggle the capturing phase. Other than that, what sort of differences are we looking at here?

jkd
12-23-2002, 06:02 PM
addEventListener allows you to add multiple events of the same type. You can't do that with property assignment without a hack.

Anyway, the primary reason is that one is correct, and the other is common practice. No where in DOM2 Events (or DOM2 HTML) does it say that events attached via property assignment should be executed.