Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks

How to check jQuery checkbox checked or not ?

Aman Jain, January 25, 2022January 25, 2022

In this article i will show you multiple way to check or validate if a checkbox is checked or not. Also i will show you to check a checkbox programmatically. In the post we will learn all possibilities to validate a checkbox and how we can check a checkbox using jQuery.

There is multiple way to check weather a checkbox is checked or not , so here are the example of jQuery checkbox checked

Using jQuery attr() function, attr() function is used to get the attribute of any element. it accepts two parameters first is name of attribute and other one is value of attribute.

If we pass only first parameter then it will return the value of attribute and both parameters are passed then it will set the attribute value.

$(element).attr('checked');  // will return string checked or null

Using jQuery prop() function

$(element).prop('checked');  // will return boolean true / false

Using jQuery is() function

$(element).is(':checked');  // will return boolean true / false

Let’s take an example of jQuery checkbox checked

Example 1 : check jQuery checkbox checked or not


In this example i will use simple checkbox and html to show their value as below

<input type="checkbox" id="isSelected" checked />
<div>Prop value : <span id="output-prop"></span></div>
<div>Attr value : <span id="output-attr"></span></div>
<div>Is value : <span id="output-is"></span></div>

<button id="mark-checked">Set Checked</button>


Here we used a checkbox with id isSelected and also the output dick to show the values and then JavaScript

<script>
  $(function () {
    $('#isSelected').click(function () {
      $('#output-prop').html($(this).prop('checked'));
      $('#output-attr').html($(this).attr('checked'));
      $('#output-is').html($(this).is(':checked'));
    });

    $('#isSelected').change(function () {
      
      $('#output-prop').html($(this).prop('checked'));
      $('#output-attr').html($(this).attr('checked'));
      $('#output-is').html($(this).is(':checked'));
    });

    $('#mark-checked').click(function () {
      var isChecked = $('#isSelected').prop('checked');
      $('#isSelected').prop('checked', !isChecked);
      $('#isSelected').trigger('change');
    });
  });
</script>

Here we registered 3 methods one for on click of checkbox, second on change of checkbox and third to check the checkbox programatically.

Live Preview :

How to check checkbox programmatically?

In the above example if you see we have registered a button and the click event on the button the programmatically check or uncheck the box. so create a button


<button id="mark-checked">Set Checked</button>

and then register click event to set the value of checkbox

$('#mark-checked').click(function () {
      var isChecked = $('#isSelected').prop('checked');
      $('#isSelected').prop('checked', !isChecked);
      $('#isSelected').trigger('change');
    });

Also Read : How to get radio input value in jQuery ?

Related

HTML Javascript jQuery checkboxHTMLjquery

Post navigation

Previous post
Next post

Related Posts

Javascript How to copy object without reference in angular

How to copy object without reference in angular ?

April 13, 2022April 14, 2022

In this tutorial we will learn to copy object without reference in angular. Objects and array are work on reference by address methodology means Objects and array keeps the address means if you change anything in the object or array from anywhere then it will change the value in original…

Read More
Javascript Laravel Multi Select Tag Autocomplete Using Select2

Laravel Multi Select Tag Autocomplete Using Select2

July 19, 2022July 19, 2022

In this article we will learn to use Laravel Multi Select Tag Autocomplete Using Select2. Select2 is useful when we want live search of bulk data or to convert the existing select boz with multi features like search, multi select and options customizations. In this article we will cover multiple…

Read More

Show dropdown list from ajax response in jQuery

September 10, 2021September 10, 2021

Showing Dynamic Select Box using jQuery and Php,MySql In this tutorial, we are going to learn the dropdown list using AJAX. Loading dropdown values from AJAX requires knowledge of javascript, jQuery, php and html. Sometimes it requires to load the dropdown like countries, state, city list dependently. In this case…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • Mapping Together: The Vibrant Spirit of OpenStreetMap Japan
  • Understanding High Vulnerabilities: A Deep Dive into the Weekly Summary
  • Building a Million-Dollar Brand: The Journey of Justin Jackson
  • Mastering Schedule Management with Laravel Zap
  • The Resilience of Nature: How Forests Recover After Fires
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version