In this tutorial we will see how to Check if Checkbox is Checked or Not in JavaScript. HTML DOM Input Checkbox checked Property can be used to check the state of a checkbox.
Table of Contents
Checked Property
The HTML DOM Checked Property either checks or unchecks the checkbox or it is used to read the state of the checkbox.
In this example checked property is used to check if checkbox is checked or not.
HTML Code
HTML Code is given below, This code contains a label tag for the checkbox and a button tag with onclick event.
<label>Checkbox</label>
<input id='chk' type="checkbox">
<br><br>
<button onclick="checkFunction()">Check If Checked or Not</button>
JavaScript Code
JavaScript Code is given below, this code will check the state of the checkbox. The code will be executed on click event of button tag.
getElementById() is used to select the checkbox using it's id. The checked property will either return true or it will return false depending on the state of checkbox.
True is returned if checkbox is checked, otherwise False is returned.
<script>
function checkFunction()
{
var checkbox = document.getElementById("chk").checked;
if(checkbox)
{
alert('Checkbox is Checked!');
}
else
{
alert('Checkbox is not Checked!');
}
}
</script>
Demo
Video Tutorial
Watch video tutorial on how to Check if Checkbox is Checked or Not using JavaScript.