1

I have a set of checkboxes that have a name like

form[check][..]

where .. is a number (id). I would have another checkbox that checked would check all the previous checkboxes, a sort of check/uncheck all. The problem is, how can with jQuery get all that checkboxes?

EDIT 1

this is my current html markup:

<input type="checkbox" value="1" name="form[check][1]" />
<input type="checkbox" value="1" name="form[check][2]" />
<input type="checkbox" value="1" name="form[check][3]" />
<input type="checkbox" value="1" name="form[check][..]" />

<input type="checkbox" name="checkAll" />
1
  • 2
    Can you post more code please? Commented Nov 25, 2012 at 21:32

2 Answers 2

2

Add a class to the checkboxes you want checked, and select them using that class

$('.myCheckbox').prop('checked', true);
Sign up to request clarification or add additional context in comments.

3 Comments

I think this is the solution with better performances. Getting all checkboxes by dynamic names seems slow
yes but this one doesn't show the OP how to control the checkboxes from the checkAll checkbox. Though I'm not sure if finding by class is much faster than specifying they are checkboxes with dynamic name
Yes, you can combine the two and go: $('[name="checkAll"]').on('change', function(){ $('.myCheckbox').prop('checked', $(this).prop('checked')); });​
1
$('[name="checkAll"]').on('change', function(){
    $('input[name^="form[check]"]:checkbox').prop('checked', $(this).prop('checked'));
});​

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.