jQuery trigger( event, [data] ) Method

« Previous Chapter Next Chapter »

Description:

The trigger( event, [data] ) method triggers an event on every matched element.

Triggered events aren't limited to browser-based events, you can also trigger custom events registered with bind.

Syntax:

Here is the simple syntax to use this method:

Example

selector.trigger( event, [data] )
                                        

Parameters:

Here is the description of all the parameters used by this method:

  • event: An event object or type to trigger.

  • data : This is an optional parameters and represents additional data to pass as arguments (after the event object) to the event handler.

Example:

Following is a simple example a simple showing the usage of this method. Here you would trigger a click event on square TWO by clicking on square ONE:

Example

<html>
<head>
   <script type="text/javascript"  src="../../js/jquery-2.2.0.js"></script>
   <script type="text/javascript" language="javascript">
   
   $(document).ready(function() {
      $("#div1").click( function () {
           $("#div2").trigger('click');
      });
      $("#div2").click( function () {
           alert( "Square TWO is clicked");
      });
   });
   </script>
   <style>
      div{ margin:10px;padding:12px;
             border:2px solid #666;
             width:60px;
           }
  </style>
</head>
<body>
   <span>Click square ONE to see the Output:</span>
   <div id="div1" style="background-color:blue;">ONE</div>
   <div id="div2" style="background-color:blue;">TWO</div>
</body>
</html>
                                        
Code It Online

« Previous Chapter Next Chapter »

Have Any Suggestion? We Are Waiting To Hear from YOU!

Your Query was successfully sent!