1 min read

Problem with JavaScript in IE7

Paul Kinlan

Paul Kinlan

Lead of Chrome DevRel

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 clickednewCheck.relatedUrl = currSearch.Query;//Expando StringnewCheck.relatedObject = currSearch; //Expand ObjectnewCheck.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 clickednewCheck.relatedUrl = currSearch.Query;//Expando StringnewCheck.relatedObject = currSearch; //Expand ObjecttagCell1.appendChild(newCheck);newCheck.checked = isChecked; //WORKS

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

Stay in the loop.

I'm trialing a newsletter. Join for monthly insights into web dev, Chrome, and the open web.

alternate_email

Get in touch

Open to chat about Chrome or Web development.

Book a consultation