views:

682

answers:

5

Using jquery, how can I:

1- Have all checkboxes on a page turned checked on or off?

2- Loop through all the checkboxes on the page which are selected. I.e something like this

$(sel-cboxes).each(myFunction);

So myFunction would be called on each selected checkbox.

Thanks in advance!

+1  A: 

1- Have all checkboxes on a page turned checked on or off?
Edited: (correction)

$(':checkbox').attr('checked',true);

2- Loop through all the checkboxes on the page which are selected. I.e something like this

$(':checkbox :checked').each(function() {

});
Marwan Aouida
$(":checkbox").atrr is not a function $(":checkbox").atrr("checked",this.checked);
Click Upvote
A: 

$("input:checked")

http://docs.jquery.com/Selectors/checked

craigsrose
+1  A: 

This jQuery Selector will find all checkboxes that have been checked. and this inside the each function will be assigned to this individual checkbox.

$(":checkbox:checked").each(function(){
  doSomething(this);
})

If you want to turn all checkboxes on then use this:

$(":checkbox").attr("checked","checked")
duckyflip
+5  A: 

1: To check all checkboxes

$("input:checkbox").attr('checked', true);

To uncheck all

$("input:checkbox").attr('checked', false);

2:

$("input:checkbox:checked").each(myFunction);
gustavlarson
input isn't necessary, just :checkbox works
Click Upvote
yes, however this is discouraged because it is slow.http://docs.jquery.com/Selectors/checkbox
gustavlarson
A: 

1: Set the all to be of the same class and then

$(.classname).attr('checked', true);
kaiz.net