[jQuery] Check all and uncheck all like the one in gmail

[jQuery] Check all and uncheck all like the one in gmail


Hello,
I have table with this structure:
<table border="0" cellpadding="0" cellspacing="0"
class="sortable-onload-5-6r rowstyle-alt colstyle-alt no-arrow"
id="main_table">
<thead>
<tr>
<th><input type="checkbox" name="checkall" id="checkall"/></th>
<th>No</th>
</tr>
</thead>
<tbody>
<tr>
<td><input type="checkbox" name="data[]" id="data_1"
value="data_1"/></td>
<td>1</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" id="data_2"
value="data_2"/></td>
<td>2</td>
</tr>
<tr>
<td><input type="checkbox" name="data[]" id="data_3 "
value="data_3"/></td>
<td>3</td>
</tr>
</tbody>
</table>
I'm using this code to simulate checkall/uncheckall like the one in gmail.
//toggle all checkbox check/uncheck
$("#checkall").click(function(){
$(":checkbox",$("#main_table")).not($("#checkall"))
.attr("checked", $(this).attr("checked"));
});
//change the top checkbox state
//when all other checkbox checked
//or one of it uncheck
$(":checkbox",$("#main_table")).not($("#checkall"))
.click(function(){
$("#checkall").attr("checked",
($(":checkbox",$("#main_table")).not($("#checkall")).length ==
$(":checkbox:checked",$("#main_table")).not($("#checkall")).length));
});
I'd like to know about my code efficiency. Is it efficient or have many
redundant selector? Is there another short and efficient way to achieve
the expected behaviour, beside using class of course :)
TIA
--
Donny Kurnia
http://hantulab.blogspot.com
http://www.plurk.com/user/donnykurnia