Problem with JavaScript in IE7

Paul Kinlan

I have been having a problem in IE7 and Internet Explorer 6, by where if you dynamically create a checkbox, set it to checked = true and then add it in to the HTML document, it loses it’s checked status.

var newCheck = document.createElement(’<input name=relatedSearchesChk>’);
newCheck.type = “checkbox”;
newCheck.onclick = updateCheckStatus;//Function to Call when clicked
newCheck.relatedUrl = currSearch.Query;//Expando String
newCheck.relatedObject = currSearch; //Expand Object
newCheck.checked = isChecked; //DOESN’t WORK. isChecked is just a local Booelan Variable.
tagCell1.appendChild(newCheck);

The closest related problem on the web that I found was this thread: Java Forums - Creating a checked Checkbox through javascript. The solution in this thread works.

However I found another solution. Update the checked status after it has been added to the document. I have no idea why it works like this but it does work.

var newCheck = document.createElement(<input name=relatedSearchesChk>’);
newCheck.type = “checkbox”;
newCheck.onclick = updateCheckStatus;//Function to Call when clicked
newCheck.relatedUrl = currSearch.Query;//Expando String
newCheck.relatedObject = currSearch; //Expand Object
tagCell1.appendChild(newCheck);
newCheck.checked = isChecked; //WORKS

If you know why this happens I would love to know: paul.kinlan@gmail.com

Paul Kinlan

Trying to make the web and developers better.

RSS Github Medium