Reputation: 3359
I have a jquery
onClick
function:
$('body').on('click', '#id_name', function() {
...
and i would like to execute this function as another function on ajax call (currently this is only onclick
).
if my ajax
call is success then:
success: function() {
// then execute top function
}
is it possible to do that?
Upvotes: 0
Views: 536
Reputation: 1362
Well, you could explicitly trigger the click event. Or even better, outsource your click code in an own function and then you can call it inside your success function. So, instead of
success: function() {
$('#id_name').trigger('click')
}
Do better this:
function clickEvent() {
// do something in here...
}
$('body').on(
'click',
'#id_name',
function (eventArgs) {
eventArgs.preventDefault();
clickEvent();
}
);
And then you can simply call it into your success callback with:
success: function() {
clickEvent();
}
Upvotes: 1
Reputation: 50291
Create a delegate function and call it separately
for example
function mainFunction(){
// Rest of code
}
on click call this
$('body').on('click', '#id_name', mainFunction)
Inside ajax success call like this
success: function() {
mainFunction()
}
Upvotes: 1
Reputation: 218828
Make the function a named (non-anonymous) function:
var someFunction = function () {
//...
};
Then you can use it for your click event:
$('body').on('click', '#id_name', someFunction);
Or your callback:
success: someFunction
Or invoke it from therein:
success: function () {
someFunction();
}
etc. Basically once you give the function a name and it's no longer anonymous you can refer to it anywhere that it's in scope.
Upvotes: 2
Reputation: 9993
Then you can use $('#id_name').trigger('click')
in the ajax success callback.
Upvotes: 5