Reputation: 79
document.getElementById("cbox0").checked = true;
I am using this statement to auto check a checkbox but i get TypeError: Cannot set property 'checked' of null for CheckBox this error. I have tried to use if statement to check if its not a null but it also give me the same error.
jsp+='<input id="cbox'+index+'" type="checkbox" name="compare" value="'+rightCard.oid+'" onchange="myFunction('+index+')"><label for="cboxCard'+index+'"> </label>';
Upvotes: 1
Views: 12950
Reputation: 6112
There could be 2 possible problems.
id
as cbox0
. Hence, getElementById
returns null
, leading to your errorDOMContentLoaded
event listener, like thisdocument.addEventListener("DOMContentLoaded", function() {
document.getElementById("cbox0").checked = true;
});
<input id="cbox0" type="checkbox" name="compare" value="test">
Note that the snippet above works even without the event listener on StackOverflow. That's probably because StackOverflow's snippet runs it's JavaScript similar to what I've written.
Upvotes: 4