jQuery checkbox goodness

blog

I was working on a select all feature today and just wanted to share this with the world. (a.k.a. keep it for later)

$(document).ready( function() {
  $('a#select_all').click( function() {
    $('input:checkbox').each( function() {
      this.checked = !this.checked;
    });
    return false;
  });
});

The first item to note is that jQuery created pseudo classes for input.

$('input:checkbox');
...
$('input:textbox');
... etc.

The second item to note, which I thought was ingenious, was setting the value of the checkbox to the inverse of it’s current value.

this.checked = !this.checked;

Works great and it’s short and sweet. Who loves jQuery?