Problem with JavaScript in IE7

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

I lead the Chrome Developer Relations team at Google.

We want people to have the best experience possible on the web without having to install a native app or produce content in a walled garden.

Our team tries to make it easier for developers to build on the web by supporting every Chrome release, creating great content to support developers on web.dev, contributing to MDN, helping to improve browser compatibility, and some of the best developer tools like Lighthouse, Workbox, Squoosh to name just a few.

I love to learn about what you are building, and how I can help with Chrome or Web development in general, so if you want to chat with me directly, please feel free to book a consultation.

I'm trialing a newsletter, you can subscribe below (thank you!)