Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Changing Classes

Edit Button is not working.. And how could it?..

When i click the edit button nothing happens. And I get that, since the editTask function is never called.. What I dont get is why it works in the video. On the addTask button we set an onclick event to trigger related function, but we never did that with the Edit and Delete buttons. Can anyone explain this to me? And I have compared my code and the downloadable code several times but I simply cant figure out what makes the button work.. :(

Here is my code:

//Problem: User interaction doesn't provide desired results.
//Solution: Add interactivty so the user can manage daily tasks.

var taskInput = document.getElementById("new-task"); //new-task
var addButton = document.getElementsByTagName("button")[0]; //first button
var incompleteTasksHolder = document.getElementById("incomplete-tasks"); //incomplete-tasks
var completedTasksHolder= document.getElementById("completed-tasks"); //completed-tasks

// New task list item

var createNewTaskElement = function(taskString){

// Create list item
var listItem = document.createElement("li");
//input (checkbox)
var checkBox = document.createElement("input");                                                
var label = document.createElement("label");                                                
    //input (text)
var editInput = document.createElement("input");                                                
var editButton = document.createElement("button");                                                
var deleteButton = document.createElement("button");                                                
    //Each elements, needs modified 

 checkBox.type = "checkbox";
    editInput.type = "text";

    label.innerHTML = taskString;

    editButton.className = "edit";
    editButton.innerHTML = "Edit";
    deleteButton.className = "delete";
    deleteButton.innerHTML = "Delete";                                               

    //and appended

return listItem;                                                


//Add a new task
var addTask = function() {
  console.log("Add task...");

  //Create a new list item with the text from #new-task:
 var listItem = createNewTaskElement(taskInput.value);

  // Append List item to incompleteTasksHolder


//Edit an existing task
var editTask = function() {
  console.log("Edit task...");

  var listItem = this.parentNode;

  var editInput = listItem.querySelector("input[type=text");
  var label = listItem.querySelector("label");

  var containsClass = listItem.classList.contains("editMode");

  //if the class of the parent is .editMode
  if(containsClass) {
    //Switch from .editMode
    //label text become the input's value
    label.innerText = editInput.value;
  } else {
    //Switch to .editMode
    //input value becomes the label's text
    editInput.value = label.innerText;

  //Toggle .editMode on the list item


//Delete an existing task
var deleteTask = function() {
  console.log("Delete task...");
  //When the Delete button is pressed
    //Remove the parent list item from the ul

//Mark a task as complete
var taskCompleted = function() {
  console.log("Task complete...");
    //Append the task list item to the #completed-tasks

   var listItem = this.parentNode;
  bindTaskEvents(listItem, taskIncomplete);

//Mark a task as incomplete
var taskIncomplete = function() {
  console.log("Task incomplete...");
    //Append the task list item to the #incomplete-tasks
 var listItem = this.parentNode;
  bindTaskEvents(listItem, taskCompleted);

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  console.log("Bind list item events");
  //select taskListItem's children
  var checkBox = taskListItem.querySelector("input[type=checkbox]");
  var editButton = taskListItem.querySelector("button.edit");
  var deleteButton = taskListItem.querySelector("button.delete");

  //bind editTask to edit button
  editButton.onclick = editTask;

  //bind deleteTask to delete button
  deleteButton.onclick = deleteTask;

  //bind checkBoxEventHandler to checkbox
  checkBox.onchange = checkBoxEventHandler;

//Set the click handler to the addTask function
addButton.onclick = addTask;

// cycle over incompleteTaskHolder ul li items
for(var i = 0; i < incompleteTasksHolder.children.length; i++) {

  bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);

    // for each list item
      // bind events to list items children ( taskCompleted)

// cycle over completeTaskHolder ul li items
for(var i = 0; i < completedTasksHolder.children.length; i++) {
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
    // for each list item
      // bind events to list items children ( taskIncomplete)

Thanks for the help :))

Samuel Webb
Samuel Webb
25,369 Points

Just made the code easier to read by adding the word javascript after the first 3 back ticks. That's how you get colors by the way.

Thanks I was wondering how to do that :D

6 Answers

Taking a quick look at the code I see a typo.

var editInput = listItem.querySelector("input[type=text"); //You are missing the closing "]" after type=text

//change to

var editInput = listItem.querySelector("input[type=text]");

Hey Miron,

I think I have an answer to your question about how the 'delete' and 'edit' buttons work. The way you declared the functions may have something to do with it. For example you usually declare a function like this...

function print(message) { document.write(message) }

But in this course he had us write the functions like this...

var print = function(message) { document.write(message) }

From what I can I tell I think that made a difference so that you didn't have to call the function to have it executed. Hope this helps.

Yes, thank you both :) It works now , and i figured it out :)

the edit button works because of the 'toggle' method.

Christopher Mlalazi
.a{fill-rule:evenodd;}techdegree seal-36
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,304 Points

@Miron Pingel where is the issue I have code exactly like yours and I can't catch the bug. Was it the missing closing ]? I also fixed that but nothing is happening can't edit added tasks or remove them as completed.

Hey Christopher,

Can you please post your code. I know you mentioned that its the exact same but just to be sure, you should post it and we can all take a look.