Aug 06 2008

Stupid IE 6 Bug #182478: Check boxes added through Javascript aren’t checked

Tag: dom, html, ie, javascript, prototype, webpmularien @ 9:44 pm

Straightforward explanation and solution of the IE checkbox bug here.

Just ran into this goofy issue recently. So, if you set the ‘checked’ attribute on a checkbox before it’s part of the DOM, in IE 6, the checkbox will not actually be checked. Brilliant!

Solution:

  • Set the checked attribute after adding the node to the DOM
  • Set the defaultChecked attribute to true prior to adding the node to the DOM

An example of this with Prototype (using solution #2, above) is:

        anElem.appendChild(new Element("input", 
                {'type':'checkbox','name':'mycb','checked':'checked','defaultChecked':'true'}));

Don’t forget to keep the ‘checked’ attribute in there so it works with other (good) browsers!

(For the overly literal among you, that isn’t really a bug number, it’s sarcasm.)