Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

jQuery : Binding events to dynamic DOM elements

DZone's Guide to

jQuery : Binding events to dynamic DOM elements

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Today in this blog post am going to discuss about a simple way of binding events to a DOM structure, especially one that changes dynamically.


Binding events to jQuery is simple. All that you need to do is use the bind function to bind a DOM element to a given event or to use the various alias functions like click, dblclick etc etc.


One of the major advantages that jQuery offers is the easy manipulation of DOM objects. That means, you can easily create, add or delete DOM elements on the fly and insert them into the page. However, doing this has its quirks as well when it comes to event binding. Lets take an example.


Consider the following HTML snippet in your page.
<div class="parent1" id="p1"><div class="parent2"><div class="child">1_c1</div><div class="child">1_c2</div></div><div class="parent2" id="1_two"><div class="child1">2_c1</div><div class="child1">2_c2</div></div></div>
Let us assume that I want to add a sibling div to the div with class "parent2" whenever a child is clicked upon. Doing that is very simple. Here's how I would do it initially.
$(".child").click(function(){
var newSibling = '<div class="child">new_child</div>';
$(this).parent().append(newSibling);
});

Well, that solves our purpose. But there is one small problem. As you see, the new div that i inserted into the DOM has a class attribute with a value "child". So, i should presumably expect that if the user clicks over the newly inserted div element, the click event handler should be triggered. Unfortunately for us, that does no happen.


It seems like the event handlers are bound to the DOM elements statically. i.e. Once the DOM is built, the selectors do not bind the same event handlers to newly introduced elements.


Now that poses a serious problem, because conceptually, it seems like the events should have been bound. So, how do we go about it?


Event bubbling to the rescue!


We know well that events in javascript are bubbled upwards in the DOM structure. This feature enables us to solve the problem of event binding for dynamically created elements.


What we are going to do is to bind the click event handler to the parent div of the "child" elements. So, whenever a child element is clicked upon, the parent div gets notified due to event bubbling. In the click handler, you can identify the source DOM element that was clicked by using the value of event.target, where event is the argument that is passed to the click handler. Once we have got a handle on the source DOM element, the job's done! So, if you add a new DOM element to the parent, since the click handler is bound to the parent, you can always be sure that the new child element will also be clickable because the actual handler function is on the parent element.


Here is the code.
$(".parent1").click(function(event){
var $source = $(event.target);
var newSibling = '<div class="child">new_child</div>';
$($source).parent().append(newSibling);
});
Pretty simple eh? Ya, I know its a dumb example, but, I guess simple is always better for demonstration purposes.


Oh, and I could have used $(this).append(newSibling); instead of $($source).parent().append(newSibling);, because it suits our requirement, but I was just trying to be a bit verbose!


Thats all for now folks!


Happy Programming :)
Signing Off Ryan Sukale

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Ryan Sukale. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}