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 Perfect

Sahan Balasuriya
Sahan Balasuriya
10,115 Points

My add task button isn't working

my add task button isn't working and i can't figure out what's wrong

//Problem: User interaction doesn't 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
    //button.edit
  var editButton = document.createElement("button"); 

    //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";


  //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 incompleteTasksHolder
  incompleteTasksHolder.appendChild(ListItem);
  bindTaskEvents(ListItem, taskIncomplete);

  TaskInput.value = "";
}  

//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 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
  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");
  //When the checkbox is checked
    //Append the 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 the checkbox is unchecked
    //Append the task list item to the #completed-tasks
  var listItem = this.parentNode;
  incompleteTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskCompleted);
}

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  console.log("Bind list items events");
  //select taskListItem's childeren
  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 the delete button
  deleteButton.onclick = deleteTask;

    //bind taskCompleted to the checkbox
  checkBox.onchange = checkBoxEventHandler;
}

//Set the click handler to the addTask function
addButton.addEventListener("click", addTask);


//cycle over 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 completeTasksHolder ul list items
for(var i =0; i < incompleteTasksHolder.children.length; i++) {
    //bind events to list item's children (taskIncomplete)
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}
<!DOCTYPE html>
<html>
  <head>
    <title>Todo App</title>
    <link href='http://fonts.googleapis.com/css?family=Lato:300,400,700' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/style.css" type="text/css" media="screen" charset="utf-8">    
  </head>
  <body>
    <div class="container">
      <p>
        <label for="new-task">Add Item</label><input id="new-task" type="text"><button>Add</button>
      </p>

      <h3>Todo</h3>
      <ul id="incomplete-tasks">
        <li><input type="checkbox"><label>Pay Bills</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
        <li class="editMode"><input type="checkbox"><label>Go Shopping</label><input type="text" value="Go Shopping"><button class="edit">Edit</button><button class="delete">Delete</button></li>

      </ul>

      <h3>Completed</h3>
      <ul id="completed-tasks">
        <li><input type="checkbox" checked><label>Go to the doctor</label><input type="text"><button class="edit">Edit</button><button class="delete">Delete</button></li>
      </ul>
    </div>

    <script type="text/javascript" src="js/app.js"></script>

  </body>
</html>

3 Answers

Hi,

I could see the "listItem" word should be as "ListItem". If you change it, it will work.

can you try it out and check

return ListItem; 
Sahan Balasuriya
Sahan Balasuriya
10,115 Points

ok i fixed that but now there's another problem when I type a task and add it the new task appears but the task's name does, its just blank

Hi Sahan,

there is a typo error in your code.

  • change the "completeTasksHolder" to completedTasksHolder.

And also validate the closing brackets in each loop once. can you also open the developer in chrome. It will give you details of the error in your code.

Thanks, Aishu