Skip to content
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
How to check & uncheck a checkbox with jQuery

How to check & uncheck a checkbox with jQuery

Posted on January 6, 2019August 3, 2021 By XpertPhp No Comments on How to check & uncheck a checkbox with jQuery

Today, We will let you know how to check & uncheck a checkbox with jQuery in this article. I see many peoples are confused about How to Check a Checkbox is Checked or Not using jquery. so I have created this article and share in our website. there is a three-way to check which checkbox is checked.

The three ways are as follows with an example.

checkbox name wise checked value

1
2
3
4
5
6
7
8
9
10
$('input[name="chk1"]').click(function(){
//checked
var chk_color = $(this).val();
var check = $(this).prop("checked");
if(check) {
alert(chk_color+" Checkbox is checked.");
} else {
alert(chk_color+" Checkbox is unchecked.");
}
});

 

checkbox class name wise checkbox checked value

1
2
3
4
5
6
7
8
9
10
$('.chk2').click(function(){
//checked
var chk_color = $(this).val();
var check = $(this).prop("checked");
if(check) {
alert(chk_color+" Checkbox is checked.");
} else {
alert(chk_color+" Checkbox is unchecked.");
}
});

 

function name wise checkbox checked value

1
2
3
4
5
6
7
8
9
function funchk3(value)
{
var check = $('.chk3').prop("checked");
if(check) {
alert(value+" Checkbox is checked.");
} else {
alert(value+" Checkbox is unchecked.");
}
}

Now, you can see our full example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<html>
        <head>
    <title>How do i know currently which checkbox is check</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
            <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
            <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
            <script>
$(document).ready(function(){
//checkbox name wise checked
$('input[name="chk1"]').click(function(){
//checked
var chk_color = $(this).val();
var check = $(this).prop("checked");
if(check) {
alert(chk_color+" Checkbox is checked.");
} else {
alert(chk_color+" Checkbox is unchecked.");
}
});
// checkbox class name wise checkbox checked
$('.chk2').click(function(){
//checked
var chk_color = $(this).val();
var check = $(this).prop("checked");
if(check) {
alert(chk_color+" Checkbox is checked.");
} else {
alert(chk_color+" Checkbox is unchecked.");
}
});
});
   // function name wise checkbox checked
function funchk3(value)
{
var check = $('.chk3').prop("checked");
if(check) {
alert(value+" Checkbox is checked.");
} else {
alert(value+" Checkbox is unchecked.");
}
}
</script>
</head>
<body>
<div class="container">
<h4 class="alert alert-success"><b>First Way:</b> "$('input[name="chk1"]')" checkbox Name wise check which checkbox is checked</h4>
<input type="checkbox" name="chk1" id="chk1red" class="chk1" value="Red" />&nbsp;<label for="chk1red">Red</label><br>
<input type="checkbox" name="chk1" id="chk1yellow" class="chk1" value="Yellow" />&nbsp;<label for="chk1yellow">Yellow</label> <br>
<input type="checkbox" name="chk1" id="chk1black" class="chk1" value="Black" />&nbsp;<label for="chk1black">Black</label><br>
<input type="checkbox" name="chk1" id="chk1white" class="chk1" value="White" />&nbsp;<label for="chk1white">White</label>
<h4 class="alert alert-warning"><b>Second Way:</b> "$('.chk2')" checkbox Class name wise check which checkbox is checked</h4>
<input type="checkbox" name="chk2" id="chk2red" class="chk2" value="Red" />&nbsp;<label for="chk2red">Red</label> <br>
<input type="checkbox" name="chk2" id="chk2yellow" class="chk2" value="Yellow" />&nbsp;<label for="chk2yellow">Yellow</label> <br>
<input type="checkbox" name="chk2" id="chk2black" class="chk2" value="Black" />&nbsp;<label for="chk2black">Black</label><br>
<input type="checkbox" name="chk2" id="chk2white" class="chk2" value="White" />&nbsp;<label for="chk2white">White</label>
<h4 class="alert alert-info"><b>Third Way:</b> "funchk3(this.value)" on click function name wise check which checkbox is checked</h4>
<input type="checkbox" name="chk3" id="chk3red" class="chk3" value="Red" onclick="funchk3(this.value)" />&nbsp;<label for="chk3red">Red</label> <br>
<input type="checkbox" name="chk3" id="chk3yellow"  class="chk3" value="Yellow" onclick="funchk3(this.value)" />&nbsp;<label for="chk3yellow">Yellow</label> <br>
<input type="checkbox" name="chk3" id="chk3black"  class="chk3" value="Black" onclick="funchk3(this.value)" />&nbsp;<label for="chk3black">Black</label><br>
<input type="checkbox" name="chk3" id="chk3white"  class="chk3" value="White" onclick="funchk3(this.value)"/>&nbsp;<label for="chk3white">White</label>
</div>
</body>
</html>

 

Demo

Please follow and like us:
error
fb-share-icon
Tweet
fb-share-icon

Recommended Posts:

  • Laravel 8 Pusher Notification Example Tutorial
  • prevent multiple clicks on submit button in jquery
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Codeigniter CRUD Operation With Ajax Example
  • Confirm Box Before Delete Item using jquery
Jquery Tags:check property, html form checkbox, javascript checker, jquery checkbox checked, jquery checkbox checked value, jquery filter, jquery tutorial, jquery uncheck checkbox, tutorials, W3C, Web development

Post navigation

Previous Post: jquery selector element with attribute name
Next Post: How to change the date formate in php

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tricks jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch Laravel Socialite laravel social login nodejs pagination payment gateway php with mysql react js tutorial rewrite rule send mail validation wysiwyg editor

Latest Posts

  • How to Convert Date and Time from one timezone to another in php
  • how to get current date and time in php
  • Drag and Drop Reorder Items with jQuery, PHP & MySQL
  • Laravel 9 Toastr Notifications Example Tutorial
  • Laravel 9 CRUD Operation Example Using Google Firebase
  • Laravel 9 CKeditor Image Upload With Example
  • Laravel 9 Summernote Image Upload With Example
  • Laravel 9 Stripe Payment Gateway Integrate Example
  • How To Send Email Using Mailtrap In Laravel 9
  • Laravel 9 Fullcalendar Ajax Example Tutorial

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Age Calculator Online
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Copyright © 2018 - 2022,

All Rights Reserved Powered by XpertPhp.com