1

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!

2
  • 1
    Name your function and call it... Commented Mar 3, 2015 at 11:50
  • 2
    It's really weird you name DOM elements "myFunction" and "callingFunction". They are not functions, they are DOM elements. You should give them id="element1", not id="myFunction". Commented Mar 3, 2015 at 11:56

3 Answers 3

2

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();
});
Sign up to request clarification or add additional context in comments.

Comments

1
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)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.