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!
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
Geoffrey Emerson18,726 Points
Can anybody explain why .on() works for the new list items while .click() only works on the original list items?
As seen in the video, we had some issues getting the dynamically created list items to recognize a click event. Why does .click() not work for all of our new list items?
Ryan FieldCourses Plus Student 21,241 Points
So when you use a method like
.on() method tells jQuery to continue to listen for new elements created that match the selector, and will bind whatever action/function you want to both elements there when the page is loaded and any new ones created after initial loading.
Craig Curtis19,985 Points
Yep, if you create new DOM elements, use an .on() event handler. Other guides also recommend using the .on() handlers over the typical click/mouse/window event functions.
Ted SumnerCourses Plus Student 17,967 Points