Event Delegation in jQuery

Event handling is the basic need to develop Rich Internet Web Applications and that will become very tough with elements added to DOM dynamically.

jQuery gives simple solution to do event binding for dynamic elements under a DOM object.

The .on() method provides several useful features.

Simple Event Binding:

Many Events but only one Event handler:

Suppose you want to trigger the same event whenever the mouse hovers over or leaves the selected elements.

Here we need to use “mouseenter mouseleave”

Many Events and Handlers:

Suppose that instead you want different event handlers for when the mouse enters and leaves an element.

if you want to show and hide a tooltip on hover, you would use this.

.on() accepts an object containing multiple events and handlers.

Event Delegation:

It will be executed for all the p tags in body tag no matter if you add few dynamically.

Ref: http://api.jquery.com/on/

The article was originally published at MicroPyramid blog.

Python, Django, Android and IOS, reactjs, react-native, AWS, Salesforce consulting & development company