jQuery | on() with Examples

The on() is an inbuilt method in jQuery which is used to attach one or more event handlers for the selected elements and child elements in the DOM tree. The DOM (Document Object Model) is a World Wide Web Consortium standard. This defines for accessing elements in the DOM tree.
Syntax:

$(selector).on(para1, para2, para3, para4)

Parameter: It accepts some parameters which are specified below-

  • para1: This specifies the events that are attached to the selected element.
  • para2: This is optional and this specify the specific child to which given event handler can be used.
  • para3: This is optional and this specifies additional data to be passed along with the function.
  • para4: This specifies the function to run when the event occurs.

Return Value: This returns all the event handler that are attached to selected element.



jQuery code to show the working of on() method:

Code #1:
In the code below child specifier and data is not passed.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
    <script src="https://ajax.googleapis.com/ajax/
             libs/jquery/3.3.1/jquery.min.js"></script>
    <script>
        <!--jQuery code to show on method -->
        $(document).ready(function() {
            $("p").on("click", function() {
                document.getElementById("p1").innerHTML = "Paragraph changed!";
            });
        });
    </script>
    <style>
        #p1 {
            font-size: 30px;
            width: 400px;
            padding: 20px;
            border: 2px solid green;
        }
    </style>
</head>
  
<body>
    <!--click on this paragraph -->
    <p id="p1">Click Here !!!</p>
</body>
  
</html>

chevron_right


Output:
Before clicking on generated output-

After clicking on generated output-

Code #2:
In the below code data and message both are being passed to the function.

filter_none

edit
close

play_arrow

link
brightness_4
code

<html>
  
<head>
              jquery/3.3.1/jquery.min.js"></script>
    <script>
        <!--jQuery code to show on method -->
        function handlerName(event) {
            var t = event.data.msg;
            document.getElementById("p1").innerHTML = t;
  
        }
  
        $(document).ready(function() {
            $("p").on("click", {
                msg: "You just clicked the given paragraph !"
            }, handlerName)
        });
    </script>
    <style>
        #p1 {
            font-size: 30px;
            width: 470px;
            padding: 20px;
            border: 2px solid green;
        }
    </style>
</head>
  
<body>
    <!--click on this paragraph -->
    <p id="p1">Click Me!</p>
</body>
  
</html>

chevron_right


Output :
Before clicking the generated output-

After clicking the generated output-



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.