0

Is it possible to have two Jquery functions like follows

$('#id1').on('click', function(){
 //body
}
$('#id2').on('click', function(){
 //body
}

but to be able to call the body of

$('#id2').on('click', function(){} );

in

$('#id1').on('click', function(){} );
cssyphus
40.4k21 gold badges104 silver badges124 bronze badges
asked Dec 2, 2013 at 20:55
0

4 Answers 4

3

Sure, you could either:

  1. Trigger click and call the function that way:

    $('#id2').click();
    
  2. Name the function and call it directly:

    function myClickFn() {
     // ..
    }
    $('#id2').on('click', myClickFn);
    $('#id1').on('click', function () {
     myClickFn();
    });
    

I would strongly recommend option #2.

answered Dec 2, 2013 at 20:57
Sign up to request clarification or add additional context in comments.

Comments

2

You can call trigger():

$("#id2").on('click', function() {
 $("#id1").trigger("click");
});

This will actually trigger the click event on that id.

answered Dec 2, 2013 at 20:58

Comments

0

Yes, have each function call a third, common, function:

$('#id1').on('click', function(){
 doCommon()
}
$('#id2').on('click', function(){
 doCommon()
}
function doCommon(){
 alert('here i am');
}
answered Dec 2, 2013 at 20:58

Comments

0

Try this

$('#id1').on('click', function(){
 //body
 $('#id2').trigger('click');
}
$('#id2').on('click', function(){
 //body
}
answered Dec 2, 2013 at 21:01

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.