views:

21862

answers:

6

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.

+6  A: 

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()
        });
Micah
you can also go $("#myTable input:checkbox").each(...);
Chris Brandsma
You can also go `$(".myCheckbox").click()`
RobertPitt
+31  A: 

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')
Xian
Is this getting the value or setting the value? How do I uncheck it?
tpower
Yes this is setting the attribute value, and uncheck by removing the attribute. I have updated my answer to show this.
Xian
good work Xian . this helped me in other tasks...
Wazdesign
+6  A: 
$("form #mycheckbox").attr('checked', true);

and if you want to check if a checkbox is checked or not:

$('form #mycheckbox').is(':checked');
bchhun
Checking for a value of 'checked' is considered more standards-compliant as per Xian's answer.
cletus
This is what I wanted to know : How to test when it is checked. Thanks.
JohnnyBizzle
+2  A: 
$("#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.

Chris Brandsma
top one will fail...checked is not a jquery object member
redsquare
i think you meant $("#mycheckbox")[0].checked for the first one, but i havent verified that for sure
Simon_Weaver
You are correct, and I fixed the error. Thank you.
Chris Brandsma
+2  A: 

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.

livefree75
+1  A: 

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

Mohamed S. Abu Emesh
Create a new question!
abatishchev
sorry i'm new to this gr8 community. but it's an answer not a question
Mohamed S. Abu Emesh