IE Bug: Input type=”checkbox” checked property
So I found a fun little bug in IE 6 just now. Don’t know if it is in IE 7 or 8.
Essentially if you have an element as shown:
<input type="checkbox" id="test-checkbox" />
And do the call:
document.getElementById("test-checkbox").checked
The result will not always reflect the checked state of the element. The reason for this is the lack of a “name” attribute in the input element. IE 6 appears to reset the state of any checkbox form element that is missing a name attribute under certain conditions. Adding the “name” attribute solves this problem.
Spent 2 hours on that gem.
Although I’m glad you have a solution… your actual code should fail regardless… the checked property is a boolean, but you aren’t setting it to a specific value (true or false).
e.g. it should be:
document.getElementById(”test-checkbox”).checked = true;
//or
document.getElementById(”test-checkbox”).checked = false;
I’m not trying to set the value, but to get it. The
document.getElementById("test-checkbox").checkedcall as given in the OP should return a true or a false. The bug is that it fails to return true/false correctly if the input element has no NAME attribute.