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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Appending and Removing Elements

JLN CRML
JLN CRML
30,362 Points

Why aren't my functions working with new added Elements?

Hi,

I tried to develop the ToDo List with jQuery, just to check my coding skills so far. All my functions are working fine, but there is one problem: When I add a new list item, no function is working on it. Like I cannot delete it or edit the label, it only works with list items, which were there before. Here is my code:

var newTask = $("#new-task");
var addButton = newTask.siblings(":button");
var incompleteTasks = $("#incomplete-tasks");
var editButton = $(".edit");
var deleteButton = $(".delete")
var completedCheck = $("input[type='checkbox']");

addButton.on("click", function (){
  var userInput = newTask.val();
  incompleteTasks.append("<li><input type=\"checkbox\"><label class=\"lbl\">" + userInput + "</label><input type=\"text\"><button class=\"edit\">Edit</button><button class=\"delete\">Delete</button></li>");
  newTask.val("");
});

editButton.on("click", function (){
  console.log("check");
if ($(this).parent().hasClass("editMode")===false) {
  $(this).siblings("input[type=\"text\"]").val($(this).siblings(".lbl").html());
  $(this).parent().addClass("editMode");
} else {
  $(this).siblings(".lbl").html($(this).siblings("input[type=\"text\"]").val());
  $(this).parent().removeClass("editMode");
}
});

deleteButton.on("click", function () {
  $(this).parent().remove();
});

completedCheck.on("change",function () {
  if($(this).prop("checked")===true) {
    $(this).parent().appendTo($("#completed-tasks"));
  } else {
    $(this).parent().appendTo($("#incomplete-tasks"));
  }
});

Hope someone can help me with that! :) Have a great day guys!

JLN CRML
JLN CRML
30,362 Points

Not sure whether this works, but you can check out the app so far here: http://port-80-2uou65vj7d.treehouse-app.com

1 Answer

Julian Aramburu
Julian Aramburu
11,368 Points

Hi Julian :D! We share names hehe D:! The problem here is that dinamically generated content wont inherit the jquery. For that to happen you can look over this article:

http://codepedia.info/2015/02/click-event-for-dynamic-button-jquery/

Hope that helps you understand it better!

Cheers and Keep Coding!

JLN CRML
JLN CRML
30,362 Points

Yay Julian ^^

Thank you so much, even though now I have to rewrite like the whole code, because my parent events aren't working anymore, anyway, this helped a lot! :)

JLN CRML
JLN CRML
30,362 Points

Update: I thought it would work but it's actually the same, I had it like this before, only changed the parameters but this has some issues as well. :/