Welcome to the Treehouse Community
Looking to learn something new?
Nathan Marshall6,019 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,089 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