jquery: How can I trigger click() without editing the element state?

Posted by user280381 on Stack Overflow See other posts from Stack Overflow or by user280381
Published on 2010-05-03T13:32:52Z Indexed on 2010/05/03 13:38 UTC
Read the original article Hit count: 176

Filed under:

I need to trigger a click function assigned to a checkbox, but without actually changing it's checked state (which click() does).

Scenario: I have a form with several checkboxes that when selected reveal a textarea. This works fine, but I also need to call the click() function on $(document).ready()

My code is below, please note I have very limited access to changing the generated html. I have an object (admin_panels) which will store each checkbox and the texarea it should bring up.

var admin_panels = {"checkbox-id" : "textarea-id"};

$(document).ready(function(){
for (var elem in admin_panels) {
    $("#" + elem).click(admin_slide);
    // $("#" + elem).click();
}
})
function admin_slide() { 
    if ($(this).is(":checked")) {
        $("#" + admin_panels[this.id] + "-wrapper").slideDown();
  }else{
        $("#" + admin_panels[this.id] + "-wrapper").slideUp();
    }
}

Thanks

© Stack Overflow or respective owner

Related posts about jQuery