views:

84

answers:

3

Welcome,

It it possible to select all checkboxes / uncheck all checkboxes what have class "xxx" ?

I can't use "name" and "ID" because there are generated dynamical via PHP and i don't know their name.

So maybye i can add class "xxx" for these what i wan't control ?

Is is possible ?

Or, if not possible. Maybye i can select all / unselect what are inside table with id "selectall" ?

Regards

+4  A: 

Here is how you can do with jquery's attr method:

$('input.xxx').attr('checked', 'checked');

This finds all input elements with class xxx and checks them all. To un-check, them, you could do like:

$('input.xxx').attr('checked', false);

For checkboxes inside a table with id selectall, you can go about like:

$('table#selectall :checkbox').attr('checked', 'checked');

and to un-check them, you should do:

$('table#selectall :checkbox').attr('checked', false);

Or:

$('table#selectall :checkbox').removeAttr('checked');
Sarfraz
Thank You :)It was really helpfully :)You solve my problem ;)
marc
@marc: You are welcome :)
Sarfraz
I have only one more question.Is it possible to use variable against xxx ?var xxx;xxx='something';And how use it here$('input.xxx')
marc
@marc - Use [`.filter()`](http://api.jquery.com/filter/), `var class = '.xxx'; $('input').filter(class).attr(...`
Nick Craver
@marc: Yes, you can use a variable there too, and you can put it in like `$('input.' + varname) `
Sarfraz
Thank you.Full, working code isfunction select_all($where){$where='.'+$where;$('input').filter($where).attr('checked', 'checked');}
marc
@marc: It is great to know that, thank you :)
Sarfraz
@marc - Make sure to [accept this as the answer](http://meta.stackoverflow.com/questions/5234/how-does-accepting-an-answer-work), same for future questions when an answer resolves your problem :)
Nick Craver
+2  A: 

To select all checkboxes with class "xxx":

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

You can use .removeAttr('checked') to unselect them.

To select all checkboxes inside an element with id "selectall":

$('#selectall :checkbox').attr('checked','checked');
Guffa
Why the `.each()`?
Nick Craver
@Nick: Good point. I already removed it before I saw your comment. :)
Guffa
A: 

If this is for a "select all" you can do something like this:

$("#selectAll").change(function() {
  $(".xxx:checkbox").attr('checked', this.checked);
});

You can test a simple demo here, when you check it, all of the .xxx checkboxes get checked, and unchecked when you unchecked it, this is usually what you want in a "Check/Uncheck All" box at the top.

Nick Craver