Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Diff between property assignment and DOM attaching for events?

    Anyone know of any practical/usable/compatible/whatever differences between assigning events as a property vs. using the addEventListener or attachEvent methods?

    Example
    Code:
    var 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?
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #2
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •