jQuery CheckBoxes

Working with selecting and unselected a group of checkboxes in jQuery can be a tad tricky.

But it appears you must use the attr to set it as shown below:

For simplicity sake, let’s say I have a list such as

Select All   Select None
[ ] Company A
[ ] Company B

Initially tried to just set the checkbox checked to true/false directly, ie. $(this).checked = true

$(“#selectAll”).click(function() {
$(“input:checkbox”).each(function() {
$(this).checked= true;
});
});

This did not work as expected.

This does works:

$(“#selectAll”).click(function() {
$(“input:checkbox”).each(function() {
$(this).attr(‘checked’, true);
});
});

$(“#selectNone”).click(function() {
$(“input:checkbox”).each(function() {
$(this).attr(‘checked’, false);
});
});

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s