How can i call a jQuery function from a jQuery function, Both the function are event handler.
My function-
$("#myFunction").live("change", function(){
//I want to call my calling function here
});
My Calling Function-
$("#callingFunction").live("change", function(){
});
How can i do this? Need help!
3 Answers 3
you can make use of .trigger() function available in jquery library to do that.
Example
$( "#foo" ).on( "click", function() {
alert( $( this ).text() );
});
$( "#foo" ).trigger( "click" );
you can also make use of change function like as below
$( ".target" ).change(function() {
alert( "Handler for .change() called." );
});
$( "#other" ).click(function() {
$( ".target" ).change();
});
Comments
function doSomething(){
/* stuff stuff stuff */
}
$("#element1").on("change", doSomething);
$("#element2").on("change", doSomething);
or
$("#element1").change(doSomething);
$("#element2").change(doSomething);
or even better, as Satpal suggested :
$("#element1, #element2").change(doSomething);
As of jQuery 1.7, live() is deprecated.
And I didn't name my elements "myFunction" and "callingFunction" as they are not functions but DOM elements.
1 Comment
$("#myFunction, #callingFunction").on('change', doSomething)If you are re-assigning the some behavior after the initial event. I'm not sure if you're looking to chain events, eg
$('ele')
.click(function(){})
.hover(function(){})
It depends on what you're doing, entirely.
id="element1", notid="myFunction".