Triggering jquery event function at page load

Posted by lazysoundsystem on Stack Overflow See other posts from Stack Overflow or by lazysoundsystem
Published on 2010-05-04T13:12:58Z Indexed on 2010/05/04 13:18 UTC
Read the original article Hit count: 175

Filed under:

Here's a little jQuery function that does what I want when a checkbox is clicked, but I have a basic gap in my knowledge:

How to register this so that it deals with checkboxes that are already clicked when the page loads?

$(document).ready(function() {
  $('fieldset input:checkbox').click(function() {
    if ($(this).attr('name') == 'foo') {
      // hide checkbox 'bar'
    }
    else {
      // show checkbox 'bar'
    }
  }
});

If I use .trigger('click'), it clicks (or unclicks) all the boxes on page load.

I can think of a few ways to do this that would involve repeating portions of the code, but I just know that jQuery already has an elegant answer for this...

© Stack Overflow or respective owner

Related posts about jQuery