I want to do something like this
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
Is such a thing built into JQuery?
To clarify, I wish to set the value.
I want to do something like this
$(".myCheckBox").checked(true);
or
$(".myCheckBox").selected(true);
Is such a thing built into JQuery?
To clarify, I wish to set the value.
you can do this:
$('.myCheckbox').attr('checked',true) //Standards compliant
or
$("form #mycheckbox").attr('checked', true)
If you have custom code in the onclick event for the checkbox that you want to fire, use this one instead.
$("#mycheckbox").click();
You can un-check by removing the attribute entirely
$('.myCheckbox').removeAttr('checked')
You can check all checkboxes like this:
$(".myCheckbox").each(function(){
$("#mycheckbox").click()
});
it certainly is
To check the checkbox (by setting the value of the checked attribute)
$('.myCheckbox').attr('checked','checked')
and un-checking (by removing the attribute entirely)
$('.myCheckbox').removeAttr('checked')
$("form #mycheckbox").attr('checked', true);
and if you want to check if a checkbox is checked or not:
$('form #mycheckbox').is(':checked');
$("#mycheckbox")[0].checked = true;
$("#mycheckbox").attr('checked', true);
$("#mycheckbox").click();
The last one will fire the click event for the checkbox, the others will not. So if you have custom code in the onclick event for the checkbox that you want to fire, use the last one.
You can also extend the $.fn object with new methods:
(function($) {
$.fn.extend({
check : function() {
return this.filter(":radio, :checkbox").attr("checked", true);
},
uncheck : function() {
return this.filter(":radio, :checkbox").removeAttr("checked");
}
});
}(jQuery));
Then you can just do:
$(":checkbox").check();
$(":checkbox").uncheck();
Or you may want to give them more unique names like mycheck() and myuncheck() in case you use some other library that uses those names.
Selects elements that have the specified attribute with a value containing the a given substring
$('input[name *= ckbItem]').attr('checked', true);
it will select all elements that contain ckbItem in it's name attribute