Stefano
Stefano

Reputation: 3259

jQuery selector for dynamic input name

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

Upvotes: 1

Views: 1570

Answers (2)

Popnoodles
Popnoodles

Reputation: 28409

$('[name="checkAll"]').on('change', function(){
    $('input[name^="form[check]"]:checkbox').prop('checked', $(this).prop('checked'));
});​

Upvotes: 1

Mladen Danic
Mladen Danic

Reputation: 3669

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

$('.myCheckbox').prop('checked', true);

Upvotes: 2

Related Questions