Saturday, November 14, 2009

Using jQuery Selectors to Select all/Select none in checkboxes

jQuery is fun! Using jQuery Selectors you can select page elements in all sorts of ways - in this example to select/unselect all checkboxes where name starts with 'chk-' (formatted 'chk-{id}'):

<a href="#" title="Select all" onclick=
"$('input[name^=chk-]').attr('checked', 'checked')">
all</a>
|
<a href="#" title="Select none" onclick=
"$('input[name^=chk-]').removeAttr('checked')">
none</a>

<input id="chk-200" name="chk-200" type="checkbox" />
<input id="chk-201" name="chk-201" type="checkbox" />
...
Gotta love jQuery!

No comments:

Post a Comment