OK, this is tested and works -- don't know why I thought .each was needed.

var tog = false; // or true if they are checked on load
 $('a').click(function() {
    $("input[type=checkbox]").attr("checked",!tog);
  tog = !tog;
 });

-- Josh

  ----- Original Message ----- 
  From: Andy Matthews 
  To: [jQuery] 
  Sent: Tuesday, October 30, 2007 9:28 AM
  Subject: [jQuery] Toggle all checkboxes, best way?


  I've got some code which I think is pretty simple. But I'd like to see if 
there's a quicker (less code) way to do this. I simply want to be able to 
toggle all checkboxes on a page on/off.

  Is there a better way?

  var $checkboxes = $('input[type=checkbox]');

  $('a').toggle(function(){
     // toggle me on
     $checkboxes.each(function(){
      $(this).attr('checked','checked');
     });
     return false;
    },function(){
     $checkboxes.each(function(){
      $(this).removeAttr('checked');
     });
    });

  ____________________________________

  Andy Matthews
  Senior ColdFusion Developer

  Office:  877.707.5467 x747
  Direct:  615.627.9747
  Fax:  615.467.6249
  [EMAIL PROTECTED]
  www.dealerskins.com

<<dealerskinslogo.bmp>>

Reply via email to