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.
$(selector).on(event, childSelector, data, function)
Parameter: It accepts some parameters which are specified below-
- event: This specifies the events that are attached to the selected element.
- childSelector: This is optional and this specify the specific child to which given event handler can be used.
- data: This is optional and this specifies additional data to be passed along with the function.
- function: 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:
In the code below child specifier and data is not passed.
Before clicking on generated output-
After clicking on generated output-
In the below code data and message both are being passed to the function.
Before clicking the generated output-
After clicking the generated output-
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- jQuery | children() with Examples
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | focusin() with Examples
- jQuery | Animation, Slide methods with Examples
- jQuery | change() with Examples
- jQuery | filter() with Examples
- jQuery | height() and innerHeight() with Examples
- jQuery | eq() with Examples
- jQuery | empty() with Examples
- jQuery | not() method with Examples
- jQuery | first() with Examples
- jQuery | parent() & parents() with Examples
- jQuery | noConflict() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : sheikh005