View Full Version : setAttribute Hassles (again!)

05-28-2003, 03:09 AM

I checked out the W3C site, and looked at the DOM2 specification (particularly: org/w3c/dom/html2/HTMLInputElement.java) because my x.setAttribute('onClick','myFunction') doesn't work for me.

I was hoping there'd be an x.onClick='myFunction' method, but there's not.

Please help, and thanks in advance.

05-28-2003, 03:30 AM
I thought I'd be clever and try translating it back to partial rubbish-code and use the x.innerHTML='...' method, but I get "unspecified" errors in both IE and Moz.

x = document.createElement('input');
x.innerHTML="class='text' type='text' name='MarginCalculator"+rowCounter+"' id='MarginCalculator"+rowCounter+"' readonly onClick='javascript: myFunction'";

myFunction is blank, and all the quotes are in the correct positions... the code seems sound. I've had it running before by writing out the entire input tag in pure HTML using innerHTML= within a td. However, I do NOT want to have to do such an ugly thing.

Please... this really needs to be fixed ASAP, or I'm gunna get reamed!

05-28-2003, 04:11 AM
x.addEventListener("click", function(event) { blablabla }, false);

Is the DOM2 way of adding events to elements.

05-28-2003, 09:41 AM
If you don't want to do the JKD way (and thus ignore ie) you can do
element.onclick=function(e,...){/*function body*/}Note the case of the "onclick", and the fact you give it a function instead of a string.