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?
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").
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.
Subscribe to our weekly Newsletter & Keep getting latest article/questions in your inbox weekly