Ali
Ali

Reputation: 267077

jQuery checkboxes manipulation

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!

Upvotes: 2

Views: 4208

Answers (5)

kaiz.net
kaiz.net

Reputation: 1994

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

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

Upvotes: 0

gustavlarson
gustavlarson

Reputation: 417

1: To check all checkboxes

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

To uncheck all

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

2:

$("input:checkbox:checked").each(myFunction);

Upvotes: 9

duckyflip
duckyflip

Reputation: 16499

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")

Upvotes: 1

craigsrose
craigsrose

Reputation: 344

$("input:checked")

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

Upvotes: 0

user434917
user434917

Reputation:

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() {

});

Upvotes: 1

Related Questions