Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Dynamically created button not working jQuery

I'm practising jQuery with this to-do app tutorial:

https://www.youtube.com/watch?v=wSnELgwFFiQ

Here is my jsfiddle:

http://jsfiddle.net/pruse/gzqben1g/

I'm able to add tasks but dynamically created delete button does not work. Any ideas what I'm doing wrong?

Thank you so much!

3 Answers

Hi tuukka uosukainen,

When we create new elements and add them to the DOM, they don't get bound by existing events we've set as jQuery only binds the event to existing elements that were in the DOM when the page loaded, to get around this we need to use what's called event delegation which is built into all modern browsers such as IE9 (older browsers such as IE8 get the same support using polyfills).

As the jQuery documentation for the .on() method shows, we can pass in a selector as the second parameter which is a string. Before we add this string we need to change our selector which in this case is $('.delete'), to a parent element which doesn't change. One element I generally like to use is body as it's always available and allows event bubbling all the way up the DOM giving our code more flexibility if we choose to expand our code structure in the future.

See the below to see how we can apply this to our code.

$('body').on('click', '.delete', deleteItem);

In the above example we can see that body is our selector we're binding our click event to, within the the on method we still have our event set as click, but now we have a new argument that is a string containing .delete which is the selector we want the browser to look for when it's clicked and when it is execute the deleteItem function.

Updated jsFiddle

Happy coding!

What a great answer Chris!

I solved the problem using live() but that did not work with all versions of jQuery. Your code seems to be working across the jQuery versions which is great.

Thank you so much for making things more clear for another newbie Chris!

You're welcome, just to be clear this version of the .on() method only works with jQuery 1.7 which isn't really a problem anymore as most sites are using jQuery 1.9+.

And just for clarity, the .live() method was the best option up until jQuery 1.6 which is when jQuery started recommending we use the .on() method for multi-version support such as you saw first hand.

Thanks for pointing these details out Chris!

Learning this stuff sure is hard but rewarding.