• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / JavaScript / Check if Checkbox is Checked in JavaScript

Check if Checkbox is Checked in JavaScript

August 10, 2022 Leave a Comment

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:

<div id="div">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox 1">
  <input type="checkbox" name="checkbox-set-1" id="checkbox-2" value="Checkbox 2" checked>
</div>

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:

<div id="div1">
  <p>Click the Checkbox to Update the "Checked" status of Checkbox</p>
  <label for="checkbox-set-1">Checkbox </label>
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox" onclick="checkCheckbox()">
  <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>

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:

<div id="div1">
  <p>Click the Checkbox to Update the "Checked" status of Checkbox</p>
  <label for="checkbox-set-1">Checkbox </label>
  <input type="checkbox" name="checkbox-set-1" id="checkbox-1" value="Checkbox" onclick="checkCheckbox()">
  <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>

<script>

function checkCheckbox(){
  var isChecked = document.getElementById('checkbox-1').checked;
  document.getElementById("checkbox-value").textContent = isChecked;
}; 

</script>

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

Other Articles You'll Also Like:

  • 1.  Using JavaScript to Check if String Contains Letters
  • 2.  Using JavaScript to Remove the Last Character From a String
  • 3.  How to Generate a Random Letter In JavaScript
  • 4.  JavaScript Coin Flip – How to Simulate Flipping a Coin in JavaScript
  • 5.  Using JavaScript to Add Items to Set
  • 6.  Using JavaScript to Set the Cursor Position
  • 7.  How to Filter a List of Divs with a Text Input Bar Using Javascript
  • 8.  Using JavaScript to Get a Random Number Between Range of Numbers
  • 9.  How to Remove Decimals of a Number in JavaScript
  • 10.  Examples Using the JavaScript += Addition Assignment Operator

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy