Use your left/right keys to browse tutorials
jQuery Event Delegation

jQuery Event Delegation

1 Star2 Stars3 Stars4 Stars5 Stars
Posted on February 2, 2010

When I first started using jQuery to make AJAX requests event delegation always caught me out as I never understood what it was or how to use it. If you’re manipulating the DOM you will surely come across the problem of when you dynamically add an element to a page you can’t just go and add an event handler to this new element and expect it to work. For instance say I have a list item which is clickable with the aid of a click event. If it’s setup so when I click the list item a completely new list item is automatically added to the page, without the use of event delegation or the function .live() the new list item won’t trigger the click function because the click function was initiated once our page was loaded, and since we added this item dynamically (meaning the page didn’t refresh) the event hasn’t been set to work with this new element.

How it works

Here’s an example of event delegation using the .live() function which is chained to our .click function.

The official jQuery documentation states: “The .live() method is able to affect html elements that have not yet been added to the DOM by using event delegation: a handler bound to an ancestor element is responsible for events that are triggered on its descendants. The handler passed to .live() is never bound to an element; instead, .live() binds a special handler to the root of the DOM tree.”

$("document").ready(function() {
    $("li").live("click", function(){
      $(this).after("<li>Click here to add another list item</li>");
    });

});

And the HTML:

<ul>
<li>Click to add a list item</li>
</ul>

You can find out more about the .live() method over at the official jQuery documentation.

demo

Thumbnail picture credit ‘Bon Jovi Concert Stage 2007′: Anirudh Koul


Recent shares

More tutorials from Papermashup
Comments
4 discussions around jQuery Event Delegation
  1. RCKY says:

    Dont get me wrong, no offense (think i missed the tone)! I’ve just remarked that… i like your blog *flowers* ;)

    • Ashley says:

      @Rcky, Hey no worries none taken! Thanks for pointing it out its a great resource. This blog is all about helping other designers and developers and building a community so feel free to post useful tips and links anytime :) (thanks for the flowers) ;)

  2. RCKY says:

    hehe i’ve seen and enjoyed the nettuts vid too…

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>



Looking for a registry cleaner to speed up your PC and show a full diagnostics?

Never miss an update from Papermashup

Get notified about the latest tutorials and downloads.

Subscribe by Email

Get alerts directly into your inbox after each post and stay updated.
Subscribe
OR

Subscribe by RSS

Add our RSS to your feedreader to get regular updates from us.
Subscribe

Get in contact

Please use the form below to get in touch.

About Me

I'm Ashley Ford, Co-founder and Technical Director at Harkable.com London, UK. Previously I worked at InMobi, Spotify and MySpace. My interests include photography and making short videos I'm also an avid F1 fan. I'm always working on side projects. Here are a few: Easy Poll, We Deliver.



What do you specialise in?

I spend a lot of time coding in PHP and MySQL, as well as front end XHTML and CSS. I also specialise in javascript and the jQuery framework as well as being an avid designer. You can find me on dribbble

Interested in advertising?

If you'd like to advertise on Papermashup.com you can find details here Or use the contact link below for further advertising opportunities.

How do I contact you

You can contact me here. and I'm available for consultation, freelance, programming book reviews.

Get on the mailing list

Join over 3000 people who have subscribed to the Papermashup inbox message, and be the first to find out about tutorial, competitions and giveaways.