rockvilla
rockvilla

Reputation: 651

How to call jquery event from other event

I have one event as,

$('#load').click(function(){
     // do something
});

then I have other event in which I want to call above event, how can I do that? Other event :

$('#save').click(function(){
    //do something related to save
    //then call $('#load').click() event in side this

});

Upvotes: 1

Views: 918

Answers (6)

Thilanka
Thilanka

Reputation: 1763

The trigger() function is the solution for your problem. You can use it as follows

$('#load').click(function(){
     // do something
});

$('#save').click(function(){
   //do something related to save
   $('#load').trigger('click');
});

Here what the code says is when you click on save it runs through your save.click event and then trigger the load.click event. You can change use any other event inside trigger() function to do trigger some other events related to "#load" element.

For more information your can refer http://api.jquery.com/trigger/

Upvotes: 0

ZloiGremlin
ZloiGremlin

Reputation: 26

Click to #load from #save.click

$('#save').click(function(){
$('#load').click()
});

Upvotes: 0

xdazz
xdazz

Reputation: 160983

$('#save').click(function(){
    //do something related to save
    //then call $('#load').click() event in side this
    $('#load').click(); // or $('#load').trigger('click');
});

Upvotes: 1

hodl
hodl

Reputation: 1420

How about just try to use your $('#load').click() event code?

Just like, when:

$('#save').click(function(){
 // Code from $('#load').click() event in side this
});

Upvotes: -1

Oyeme
Oyeme

Reputation: 11235

You can do it like this:

$('#load').click(function(){
     make();
});

$('#save').click(function(){
   make();
});
function make(){

}

Upvotes: 1

elclanrs
elclanrs

Reputation: 94151

You can use trigger():

$('#load').trigger('click');

Upvotes: 3

Related Questions