To check if a checkbox is checked in JavaScript, the simplest way is to target the checkbox using a method like getElementById() and then check the checked property of the element to see if it is true or false.

document.getElementById("checkboxInput").checked

We can then put this in an if-else statement.

var isChecked = document.getElementById("checkboxInput").checked;

if( isChecked == true){
  //Checkbox is checked
} else {
  //Checkbox is NOT checked
}

Let’s say we have the following HTML:


If we want to see if the checkboxes are checked, we can use the following code:

var isChecked1 = document.getElementById('checkbox-1').checked;
console.log(isChecked1); 

#Output
false

var isChecked2 = document.getElementById('checkbox-2').checked;
console.log(isChecked2);

#Output
true

Not that we can also easily check if a checkbox is checked using jQuery.

Check if Checkbox is Checked Using JavaScript When Checkbox is Clicked

We can check if a checkbox is checked using JavaScript very easily by checking the checked property with a click event.

Let’s say we have the following HTML code and we want to display the checked value of our checkbox in the span below:

Click the Checkbox to Update the “Checked” status of Checkbox

The value of the checkbox is: .


We can utilize an onclick event and the getElementById() method to find the value of the checkbox. Then we will change the text of the span and display true or false.

Below is the JavaScript code which will allow us to see if our checkbox is checked and then display that value.

function checkCheckbox(){
  var isChecked = document.getElementById('checkbox-1').checked;
  //Get the checked property value of the checkbox

  document.getElementById("checkbox-value").textContent = isChecked;
  //Display that value to the user
}; 

The final code and output for this example of how to check if a checkbox is checked using Javascript is below:

Code Output:

Click the Checkbox to Update the “Checked” status of Checkbox


The value of the checkbox is: .

Full Code:

Click the Checkbox to Update the “Checked” status of Checkbox

The value of the checkbox is: .




Hopefully this article has been useful for you to understand how to check if a checkbox is checked in JavaScript.

Categorized in:

JavaScript,

Last Update: May 3, 2024