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
If you want to call the
admin_slidefunction for an element, you can do this:The value of
thiswithin theadmin_slidefunction will be set todomElement.In this case,
domElementcould be$('#'+elem)[0]ordocument.getElementById(elem).Edit:
Another way to do this, using jQuery methods exclusively, is:
$(selector).each(admin_slide). So your code could be: