MorningZ code looks good, except that it doesn't check 'Check all'
automatically when you check all boxes by hand.

You're creating lots of jQuery objects unnecessarily:

var allBoxes = $('#main_table :checkbox:not(#checkall)');

$("#checkall").click(function(){
  allBoxes.attr("checked",this.checked);
});
// you can also write $(':checkbox','#main_table'), jQuery will take
care of interpreting the context for you.

allBoxes.click(function(){
   var check = (allBoxes.length == allBoxes.filter
(':checked').length) ? 'checked' : '';
   $('#checkall').attr('checked', check);
});

cheers,
- ricardo

On Dec 10, 6:30 am, Donny Kurnia <[EMAIL PROTECTED]> wrote:
> 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 Kurniahttp://hantulab.blogspot.comhttp://www.plurk.com/user/donnykurnia

Reply via email to