chicharito
chicharito

Reputation: 1087

Automatic call of on-click function in jquery

I am looking to call a onclick function forcefully.

$('.checkbox-selector').click(function() {

});

$('.checkbox-selector1').click(function() {

});

When a control goes to the first function, the second function should be called automatically i.e. onlick event is triggered.

Upvotes: 0

Views: 572

Answers (3)

Tim S.
Tim S.

Reputation: 13843

function func1(e) {
    // do stuff for selector

    // run func2 too!
    func2();
}

function func2(e) {
    // do stuff for selector1
}

$('.checkbox-selector').click(func1);
$('.checkbox-selector1').click(func2);

Is this what you mean?


If so, make sure to look at the comments! They contain quite valuable information considering events and such.

You can replace func2(); with $('.checkbox-selector1').trigger('click'); to trigger the native event handler too! Using $('.checkbox-selector1').triggerHandler('click'); is practically the same as func2();, whichever you prefer.

Upvotes: 3

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76405

Not sure what it is exactly you're looking for, but I'd guess:

$('.checkbox-selector').click(function() {
    /* all sorts of stuff*/
    $('.checkbox-selector1').click();
    //or:
    $('.checkbox-selector1').trigger('click');
});

$('.checkbox-selector1').click(function() {

});

Something like that?

Upvotes: 0

René
René

Reputation: 6176

Take a look at the jQuery trigger function

Upvotes: 1

Related Questions