[thelist] JS: DOM generated page, IE & setAttribute() = failure

Frank lists at frankmarion.com
Wed Jun 27 00:50:35 CDT 2007


I've used DOM to programmatically build a chunk of page (several 
paragraphs, a form and some fields). The fields have error checking 
that when a user inputs something out of range pops up an error 
message and changes the backgroundColor of the field to red.

The following works correctly in FF, Opera and Safari

        iptYear.setAttribute("onClick","clearField(this);")

I know setAttribute() is borked on IE (based on my 
research).  Considering that the whole chunk of document is 
programmatically generated, how can I add an onclick event to a field 
that will both correctly trigger a function and pass it it's id? (so 
that clearField(obj) can reset the style and edit the value)

Sample (of simplified) code below

         // HTML structure: INPUT. Year input.
         iptYear = document.createElement("input");
         iptYear.setAttribute("type","text");
         iptYear.setAttribute("name",'form_field_year');
         iptYear.setAttribute("id",'form_field_year');
         iptYear.setAttribute("value",Now.getFullYear());
         iptYear.setAttribute("onClick","clearField(this);")

         // Some of the multiple variations that fail...
         // iptYear.setAttribute('"onclick", "clearField(this)");
         //iptYear.setAttribute("onClick","clearField(document.getElementById('form_field_year'))");
         //iptYear.onclick = 
clearField(document.getElementById('form_field_year'));

         f.appendChild(iptYear); // (f is the form)

You can see it at work here:
         http://www.alladdictionsanonymous.com/tools/

Try entering 2008 in the year field...

Thanks



Frank Marion     lists at frankmarion.com      Keep the signal high.






More information about the thelist mailing list