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
Nathan Marshall6,031 Points
The loop will not iterate through multiple tasks and give them the class of 'checked'
I have managed to get to the stage where I can add the class to the second task once added but not the first.
I am now at a dead end, I really wanted to do this without any help but I'd rather understand what I'm doing wrong at this point.
Here is my CodePen: https://codepen.io/Nathan-Callum-Marshall/pen/qKJPRV?editors=1010
Help/Guidance would be appreciated.
in that case, try to put your add event listener declaration inside functions that you can call every time after you create new elements
Hi Nathan, you need to add the needed event listeners to all the new elements after they've been created
Balazs Peak46,160 Points
I think the problem is that you are adding even-listeners at the beginning, and adding the tasks later, but the new tasks do not have the event listener.
What I would do is putting all tasks in a container, and setting up the listener at the beginning for the container: if the container got clicked, the clicked child element would get the specific actions, for example. (You can do that with the even target thing.)
check this question in stackoverflow: https://stackoverflow.com/questions/203198/event-binding-on-dynamically-created-elements
with the jQuery method .on(), you just need to declare your add event listener function once and it will be applied for every new elements