How to trigger other elements event handler with jQuery
jQuery comes with a trigger() function to execute the event handlers that attached to elements. For instance,
A single click event bind to a button with an Id of “button1″.
$("#button1").bind("click", (function () {
alert("Button 1 is clicked!");
}));A single click event bind to a button with an Id of “button2″. and a trigger to execute the button1 click event handler.
$("#button2").bind("click", (function () {
alert("Button 2 is clicked!");
$("#button1").trigger("click");
}));When button2 is clicked, the alert message “Button 2 is clicked!” is prompt, follow by button1 alert message “Button 1 is clicked!‘.
Try it yourself
<html> <head> <script type="text/javascript" src="jquery-1.4.2.min.js"></script> </head> <body> <h1>jQuery trigger() example</h1> <script type="text/javascript"> $(document).ready(function(){ $("#button1").bind("click", (function () { alert("Button 1 is clicked!"); })); $("#button2").bind("click", (function () { alert("Button 2 is clicked!"); $("#button1").trigger("click"); })); }); </script> </head><body> <input type='button' value='Button 1' id='button1'> <input type='button' value='Button 2' id='button2'> </body> </html>