How do I determine if a checkbox is checked?
Place the var lfckv
inside the function. When that line is executed, the body isn't parsed yet and the element "lifecheck"
doesn't exist. This works perfectly fine:
function exefunction() {
var lfckv = document.getElementById("lifecheck").checked;
alert(lfckv);
}
<label><input id="lifecheck" type="checkbox" >Lives</label>
<button onclick="exefunction()">Check value</button>
You are trying to read the value of your checkbox before it is loaded. The script runs before the checkbox exists. You need to call your script when the page loads:
<body onload="dosomething()">
Example:
http://jsfiddle.net/jtbowden/6dx6A/
You are also missing a semi-colon after your first assignment.