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: Changing Classes

Ashley Galante
Ashley Galante
4,668 Points

Toggle not working properly

When I click on the 'edit' button for the second time, it doesn't change back to the read-only mode. Can anyone tell me what I'm doing wrong?

//Problem: User interaction doesnt provide desired results.
//Solution: Add interactivity 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"); //checkbox
   //label
   var label = document.createElement("label");
   //input (text)
   var editInput = document.createElement("input"); //text
   //create button.edit
   var editButton = document.createElement("button");
   //create button.delete
   var deleteButton = document.createElement("button");

   //each element needs modifying
   checkBox.type = "checkbox";
   editInput.type= "text";

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

   label.innerText = taskString;

   //each element needs appending
   listItem.appendChild(checkBox);
   listItem.appendChild(label);
   listItem.appendChild(editInput);
   listItem.appendChild(editButton);
   listItem.appendChild(deleteButton);

   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 listItem to incompleteTaskHolder
  incompleteTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskCompleted);
}

//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
    } else {
      //switch to .editMode
      //input value becomes the label's text
      editInput.value = label.innerText;

    //toggle .editMode on the list item
      listItem.classList.toggle("editMode");
    }
}

//Delete an existing task
var deleteTask = function() {
  console.log("Delete task...")
  var listItem = this.parentNode;
  var ul = listItem.parentNode;

  //remove the parent list item from the ul
  ul.removeChild(listItem);

}

//Mark a task as complete
var taskCompleted = function() {
  console.log("Task complete...")
  //append task list item to the #completed-tasks
  var listItem = this.parentNode;
  completedTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskIncomplete);
}

//Mark a task as incomplete
var taskIncomplete = function() {
  console.log("Task incomplete...")
  //when checkbox is unchecked

    //append the task list to #incomplete-tasks
  var listItem = this.parentNode;
  incompleteTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskCompleted);
}




var bindTaskEvents = function(taskListItem, checkboxEventHandler) {
  console.log("bind list item events")
  //select its children
    var checkBox = taskListItem.querySelector("input[type=checkbox]");
    var editButton = taskListItem.querySelector("button.edit");
    var deleteButton = taskListItem.querySelector("button.delete");
    //bind the editTask to edit button
    editButton.onclick = editTask;
    //bind deleteTask to the delete button
    deleteButton.onclick = deleteTask;
    //bind checkboxEventHandler to the checkbox
    checkBox.onchange = checkboxEventHandler;
}

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

//cycle over the incompleteTasksHolder ul list items
for(var i = 0; i < incompleteTasksHolder.children.length; i++) {
    //bind events to list item's children (taskCompleted)
  bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}

//cycle over the completeTasksHolder ul list items
for(var i = 0; i < completedTasksHolder.children.length; i++) {
    //bind events to list item's children (taskIncomplete)
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}

1 Answer

Hi Ashley,

You have your code to toggle the class inside your else block.

    } else {
      //switch to .editMode
      //input value becomes the label's text
      editInput.value = label.innerText;

    //toggle .editMode on the list item
      listItem.classList.toggle("editMode");
    } //this curly brace ends the else block. Move it above the code to toggle the class.
}

Moving it outside the else will allow the class to toggle every time the function runs.