Event Handling in jQuery
Event Handling
jQuery provides an efficient way to add an event handler. Events occur when the user performs an action like clicking an element, moving the mouse, key pressing, key up, key down, or submitting a form. When an event occurs on a target element, a handler function is executed. The advantage of using jQuery is that the code is much shorter and easy to write as compared to JavaScript. In the example, we attach a click event handler to the p tag.
Syntax:-
Its syntax is:- $(selector).on(event,childSelector,data,function,map)
Further Explanation:-
Parameter | Description |
---|---|
event | Required. Specifies one or more event(s) or namespaces to attach to the selected elements. |
childSelector | Optional. Specifies that the event handler should only be attached to the specified child elements. |
data | Optional. Specifies additional data to pass along to the function |
function | Required. Specifies the function to run when the event occurs |
map | Specifies an event map containing one or more events to attach to the selected elements. |