IE setAttribute onclick

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    #1

    IE setAttribute onclick

    yea i know there is millions of stuff about this on the net, and this isnt really a question but just wanted to see if people had picked up anything similar.

    This is only for internet explorer, firefox doesnt have any problem.

    Code:
    i = 666;
    
    
    myImg = document.createElement("img");
    myImg.setAttribute('onClick', 'doSomething(\'' +i+ '\');'); //this works
    
    myDiv = document.createElement("div");
    myDiv.setAttribute('onClick', 'doSomething(\'' +i+ '\');');  //this doesn't

    so setAttribute for the onclick event works but not for Divs.

    Anybody know why? Wouldnt this take extra coding to stop the function working?

    this is the work around ive used anyway; after checking if the user is on IE:

    Code:
    myDiv.onclick = new Function( "return dosomething('" + i + "');" );

    god i hate IE.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Use addEventListene r (W3C standard). Of course, this won't work in IE, so use its own proprietary attachEvent method. See this link.

    Also, check out this article where volectricity has normalized the two forms.

    Comment

    Working...