How to check or uncheck checkbox using jQuery?


I have some filters on page so when clicking on "x" button of a selected filter, need to remove the filter and uncheck the checkbox using jQuery, so How can I do that on dynamically created element using jQuery?


Asked by:- Vinnu
0
: 1720 At:- 4/6/2019 8:01:37 AM
jQuery HTML check-uncheck-checkbox







2 Answers
profileImage Answered by:- manish

jQuery prop() method can be used to check or uncheck a checkbox dynamically

The prop() method require jQuery 1.6 and above, here is the sample

$('#YouCheckboxId').prop('checked', true); // Check
$('#YouCheckboxId').prop('checked', false); // Uncheck, may not work in some browsers

$('#YouCheckboxId').removeAttr('checked'); //uncheck, by removing attribute

Below jQuery 1.6 you can use .attr()

$('#YouCheckboxId').attr('checked', true); // Check 
$('#YouCheckboxId').attr('checked', false); // Uncheck

Note: An element is considered checked as long as it has the attribute of checked even if it has no value or the value is like 'false' or whatever, so to uncheck the checkbox, it is safe to use attribute removeAttr("checked").

1
At:- 4/9/2019 10:56:05 AM


profileImage Answered by:- pika

You can have custom function like below for check using

$('.myCheckbox').attr('checked',true)

and uncheck using 

$('.myCheckbox').removeAttr('checked')

For jQuery 1.6 + use .prop

$('.myCheckbox').prop('checked', true); // Checks it
$('.myCheckbox').prop('checked', false); // Unchecks it

Plain Javascript, using ClassName

var elements = document.getElementsByClassName('myCheckBox');
for(var i = 0; i < elements.length; i++)
{
    elements[i].checked = true;
}

Make it 'false' to unchecking checkbox.

1
At:- 6/3/2022 9:32:20 AM
Thanks for providing Pure Javascript based solution 0
By : Vinnu - at :- 6/22/2022 6:56:28 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use