Back to Top

Event Delegation in jQuery

Event Delegation in jQuery

In the learning process of jQuery, I found the bind event of jQuery which you can use with bind() method or can use on() method, someone use delegate() or live(). But all do the same process that is Event Delegation.I have used all but do not fully understand the difference between all.

So, The Simple reason of so many types of binding method is the updated version of jQuery added on () method for version 1.7 and later.

If you are using jQuery constantly for your web application, You may have heard about the event delegation concept of jQuery. Event Delegation concept is very convoluted design pattern which is useful when you want to add the event to an element which are the child element of any element.

What is Event Delegation?

DOM Structure of scripting language is complex to understand.Event Delegation is an optimization technique for event binding of DOM element by using the principle of event bubbling through binding events to the parent element.

SYNTAX:

New Delegate Function Syntax:

Example:

Parameters

Events: You can enter one or more space-separated event types such as “click”.
Selector: A selector string for the descendant selector filter element triggering event.
Data: is optional.When an event is triggered, data will be passed to the event handler.
Fn: Function called when the event is triggered.

.on() event delegation method to bind the currently selected elements with the jQuery object. Before jQuery 1.7, .bind(),.delegate() and .live() three different methods were used for the event delegation in jQuery. Now, only .on() method is used and provides all the functionality required to bind event handlers.To attach an event that runs only once, you can use the .one () method.

Official documentation suggests:

As of jQuery 1.7, the .on () method is the preferred method for attaching event handlers to a document.

.bind() method is directly bind the element whereas .live() method is a way to bind by bubbling elements. But .delegate () is a more accurate than the .live () method and .on() method is a new binding mechanism.

As above code, the click event bind to the document object and when the click event occurs on any element of the page to be processed, propagate the document object.

Let’s check below HTML Code:

Here in the html, placed a div and several p tags inside the div element.When to click the p elements. P elements on the page we will bind click event.

Now following jQuery code will perform Event Delegation on the div with parent element id. Here I am going to bind the event to all elements which are the child of div.

Now lets see the actual code:

Using delegate method, you can accurately locate the event and event bubbling overhead can be reduced.If this article helped you, then don’t forget to share it on Facebook and join thecreativedev on Facebook.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

AngularJS Routing Using UI-Router

Posted on 8 years ago

Bhumi

How to use HTML5 Page Visibility API

Posted on 9 years ago

Bhumi

How to Add Tag in WordPress

Posted on 12 years ago

Bhumi