Generating Dynamic Checkboxes – Internet Explorer
Recently I was working on web pages which required dynamic generation of check boxes at the client side. One such page was having a table of data and a list of check boxes generated dynamically at the time of loading of that page. Each check box was acting as a filter based on its state(checked/unchecked) controlling the display of some of the rows in the table . The JavaScript code had a function to generate the list of check boxes.
function insertChild(parent, child, group) {
var listelem = document.createElement("li");
var input = document.createElement("input");
input.name = group;
input.type = "checkbox";
listelem.id = child;
listelem.appendChild(input);
input.value = child;
input.checked = true;
input.onclick = function() { toogleCheckBox(this); }
var label = document.createElement("label");
label.innerHTML = child;
listelem.appendChild(label);
parent.appendChild(listelem);
}
When the state of any checkbox was changed, another function filterTable() was called. It extracted the value of that checkbox and used it to filter out some rows of the table. It perfectly worked in Firefox but not in internet explorer. The value of the checkbox extracted inside filterTable() was always either ‘On’ or ‘Off’ in IE. Finally after some googling, I found out that the ‘On’ and ‘Off’ values are used in IE when no value is explicitly assigned to the checkbox. So the problem was with insertChild() which tried to assign a value to the checkbox even before the checkbox was appended to the document. I changed the code [red lines] and it started working in IE. Thus, it is always better to append the newly created node to the document first before editing its properties.
People Say