How to Check a Checkbox with jQuery?
Last Updated :
20 Nov, 2024
We can directly select the element by it's selector and then we can use checked property on that for check and unchecked.
1. By using Checked Property
In this method, we will select the element using the element selector and directly use the checked property on the item that is present at the passed index of the selected elements, and set its value to true.
Syntax
$('element_selector')[index].checked = true;
Example: The below example will explain the use of the checked property to check the checkbox using jQuery.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://code.jquery.com/jquery-2.2.4.min.js">
</script>
</head>
<body>
<p>
<input type="checkbox" class="checkboxes">
Front-End
<input type="checkbox" class="checkboxes">
Back-End
</p>
<p>
<button type="button" class="check-front">
Subscribe Front-End
</button>
<button type="button" class="check-back">
Subscribe Back-End
</button>
<button type="button" class="reset">
Reset
</button>
</p>
<script type="text/javascript">
$(document).ready(function () {
$(".check-front").click(function () {
$(".checkboxes")[0].checked = true;
});
$(".check-back").click(function () {
$(".checkboxes")[1].checked = true;
});
$(".reset").click(function () {
$(".checkboxes")[0].checked = false;
$(".checkboxes")[1].checked = false;
});
});
</script>
</body>
</html>
Output:
2. Using prop() Method
The input can be accessed and its property can be set by using the prop() method. This method manipulates the ‘checked’ property and sets it to true or false depending on whether we want to check or uncheck it.
Syntax
$("element").prop("checked", true)
Example: The below example uses the prop() method of jQuery to check the checbox.
html
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"https://code.jquery.com/jquery-2.2.4.min.js">
</script>
</head>
<body>
<p>
<input type="checkbox" name="option" id="front">
Front-End
<input type="checkbox" name="option" id="back">
Back-End
</p>
<p>
<button type="button" class="check-front">
Subscribe Front-End
</button>
<button type="button" class="check-back">
Subscribe Back-End
</button>
<button type="button" class="reset">
Reset
</button>
</p>
<script type="text/javascript">
$(document).ready(function () {
$(".check-front").click(function () {
$("#front").prop("checked", true);
});
$(".check-back").click(function () {
$("#back").prop("checked", true);
});
$(".reset").click(function () {
$("#front").prop("checked", false);
$("#back").prop("checked", false);
});
});
</script>
</body>
</html>
Output
3. Using attr() Method
It is similar to the above method and more suitable for older jQuery versions. The input can be accessed and its property can be set by using the attr() method. We have to manipulate the ‘checked’ attribute and set it to true or false depending on whether we want to check or uncheck it.
Syntax
$("element").attr("checked", true)
Example: The below code example implements the attr() method to check the checkbox.
html
<!DOCTYPE html>
<html lang="en">
<head>
<script src=
"https://code.jquery.com/jquery-2.2.4.min.js">
</script>
</head>
<body>
<p>
<input type="checkbox" name="option" id="Front">
Front-End
<input type="checkbox" name="option" id="Back">
Back-End
</p>
<p>
<button type="button" class="check-Front">
Subscribe Front-End
</button>
<button type="button" class="check-Back">
Subscribe Back-End
</button>
<button type="button" class="reset">
Reset
</button>
</p>
<script type="text/javascript">
$(document).ready(function () {
$(".check-Front").click(function () {
$("#Front").attr("checked", true);
});
$(".check-Back").click(function () {
$("#Back").attr("checked", true);
});
$(".reset").click(function () {
$("#Front").attr("checked", false);
$("#Back").attr("checked", false);
});
});
</script>
</body>
</html>
Output
Similar Reads
How to Check whether a Checkbox is Checked in jQuery? We can use prop() and is() methods to check whether a checkbox is checked in jQuery. A checkbox is a user interface element that allows users to select or deselect an option. 1. Using prop() MethodThis prop() method provides a simple way to track down the status of checkboxes. It works well in every
2 min read
How to check a radio button with jQuery? There are two methods by which one can dynamically change the currently selected radio button by changing the checked property of the input type. Method 1: Using prop method: The input can be accessed and its property can be set by using the prop method. This method manipulates the 'checked' propert
3 min read
How to change the checkbox value using jQuery ? The Checkboxes are used to let a user select one or more options for a limited number of choices. The :checkbox selector selects input elements with type checkbox.Syntax: $('#textboxID').val($("#checkboxID").is(':checked')); In the above syntax, basically the return value of the checked or unchecked
2 min read
How to find all checkbox inputs using jQuery ? The task is to find all the checkbox input using jQuery. Checkboxes are the square boxes that are ticked when activated, and it is used to select one or more number of choices. Method and Selectors used: :checkbox: This selector is used to select the input element with type = checkbox. .wrap(): This
2 min read
How to hide the checkbox based on value in jQuery? The checkbox is used to set the content according to the user which he wants to see at the interface. Users also can set multiple choices according to their wishes.Syntax:$('input[name=foo]').attr('checked', false);ApproachIntegrate jQuery in HTML file via CDN Links. Includes the necessary title. Ad
2 min read
jQuery UI Checkboxradio widget() Method jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. Whether youâre building highly interactive web applications, or you just need to add a date picker to a form control, jQuery UI is a perfect choice. In this article,
1 min read
How to make a Themed Checkbox using jQuery Mobile ? jQuery Mobile is a web based technology used to make responsive content that can be accessed on all smartphones, tablets and desktops. In this article, we will be creating a Themed Checkbox using jQuery Mobile. Approach: Add jQuery Mobile scripts needed for your project. <link rel=âstylesheetâ hr
1 min read
EasyUI jQuery checkbox widget In this article, we will learn how to design a checkbox using jQuery EasyUI. The checkbox widget is used for selecting multiple choices. Each choice can be enabled by clicking on the boxes. EasyUI is an HTML5 framework for using user interface components based on jQuery, React, Angular, and Vue tech
2 min read
How to Check/Uncheck the checkbox using JavaScript ? To check and uncheck the checkbox using JavaScript we can use the onclick event to toggle the checkbox value. We can also check or uncheck all the checkboxes on loading the webpage using JavaScript onload function.In this article, we will learn how to check/uncheck the checkbox using JavaScript. As
3 min read
jQuery :checkbox Selector The jQuery :checkbox Selector is a very important element to make interactions with a user. In JQuery, we can build a simple checkbox to allow data entry as well. Syntax: $(":checkbox")We can implement the checkbox in the code: Example 1: In this example, we will select the input element which has a
1 min read