• 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 / jQuery / Using jQuery to Check if Checkbox is Checked

Using jQuery to Check if Checkbox is Checked

November 30, 2021 Leave a Comment

To check if a checkbox is checked using jQuery, the simplest way is to use is() combined with “:checked”.

$('#checkbox').is(':checked');

Let’s say I 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 I want to see if the checkboxes are checked, we can do the following:

var isChecked1 = $('#checkbox-1').is(':checked');
console.log(isChecked1); // false

var isChecked = $('#checkbox-2').is(':checked');
console.log(isChecked2); // true

If you are using WordPress, don’t forget to change the $ to jQuery as below:

var isChecked1 = jQuery('#checkbox-1').is(':checked');
var isChecked2 = jQuery('#checkbox-2').is(':checked');

Check if Checkbox is Checked Using jQuery When Checkbox is Clicked

Many times when creating a web page and the user experience, we want to check certain conditions based on an interaction with another element on the web page.

We can check if a checkbox is checked using jQuery very easily by combining the is() method 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">
    <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>

We can utilize both the jQuery click() method and jQuery is() 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.

$('#checkbox-1').click(function(){
    $("#div1 p span").text($('#checkbox-1').is(':checked'));
}); 

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

Code Output:

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


The value of the checkbox is: false.

Full Code:

<div class="html-code-output">
<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">
    <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>
</div>

<script>

$('#checkbox-1').click(function(){
  $("#div1 p span").text($('#checkbox-1').is(':checked'));
});

</script>

Using the jQuery prop() method to Check if a Checkbox is Checked

Another way we can check if a checkbox is checked with jQuery is using the jQuery prop() method.

Let’s say we have the same HTML as above:


<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">
    <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>

Below is the Javascript code which will allow us to see if our checkbox is checked using the prop() method.

$('#checkbox-1').click(function(){
    $("#div1 p span").text($('#checkbox-1').prop('checked'));
}); 

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

Code Output:

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


The value of the checkbox is: false.

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">
  <p>The value of the checkbox is: <span id="checkbox-value"></span>.</p>
</div>

<script>

$('#checkbox-1').click(function(){
    $("#div1 p span").text($('#checkbox-1').prop('checked'));
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Move Element Before Another
  • 2.  Using jQuery to Remove Attribute from HTML Element
  • 3.  Using jQuery to Remove All Options From Select
  • 4.  Using jQuery to Replace HTML Inside Div
  • 5.  jQuery Get Last Child
  • 6.  jQuery Random Number Generator Between Two Numbers
  • 7.  jQuery siblings – Get the Sibling Elements of a Div
  • 8.  Using jQuery to Delete an Element
  • 9.  jQuery Compare Dates
  • 10.  jQuery rotate – How to Rotate an Image using jQuery

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