Friday, July 31, 2009

Jquery for Select All Checkbox

A very common interface is a list of items with a checkbox to select each and them you perform an action on those selected. Typically you have a checkbox at the top to toggle all of the list. This script will take care of everything for you. You can exclude elements by checking the ID or name properties.

$(document).ready(function() {
$("#selectAllCheckBox").click(function() {
var checked_status = this.checked;
$("input[type=CheckBox]").each(function() {
if (this.id != "pleaseSkipMe") {
this.checked = checked_status;
}
});
});
});

2 comments:

thorn said...

Using jQuery you don't need to write so mush code.

$(function() {
$("#selectAllCheckBox").click(function() {
$(':checkbox').not('#pleaseSkipMe').attr('checked', this.checked);
});
});

PretzelSteelersFan said...

@Thorn -- Thanks. I'll get it updated soon.