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
4 Answers 4
Sure, you could either:
Trigger
clickand call the function that way:$('#id2').click();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
Andrew Whitaker
126k32 gold badges296 silver badges308 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
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
cssyphus
40.4k21 gold badges104 silver badges124 bronze badges
Comments
Try this
$('#id1').on('click', function(){
//body
$('#id2').trigger('click');
}
$('#id2').on('click', function(){
//body
}
Comments
lang-js