setAttribute, onClick and cross browser compatability...

Posted by Nicholas Kreidberg on Stack Overflow See other posts from Stack Overflow or by Nicholas Kreidberg
Published on 2009-04-14T19:10:25Z Indexed on 2010/04/24 16:23 UTC
Read the original article Hit count: 449

Filed under:
|
|

I have read a number of posts about this but none with any solid answer. Here is my code:

// button creation
onew = document.createElement('input');
onew.setAttribute("type", "button");
onew.setAttribute("value", "hosts");
onew.onclick = function(){fnDisplay_Computers("'" + alines[i] + "'"); }; // ie
onew.setAttribute("onclick", "fnDisplay_Computers('" + alines[i] + "')"); // mozilla
odiv.appendChild(onew);

Now, the setAttribute() method (with the mozilla comment) works fine in mozilla but only if it comes AFTER the line above it. So in other words it seems to just default to whichever gets set last. The .onclick method (with the ie comment) does not work in either case, am I using it incorrectly?

Either way I can't find a way to make this work at all in IE, let alone in both. I did change the function call when using the .onclick method and it worked fine using just a simple call to an alert function which is why I believe my syntax is incorrect.

Long story short, I can't get the onclick parameter to work consistently between IE/Mozilla.

-- Nicholas

© Stack Overflow or respective owner

Related posts about dom

Related posts about AJAX