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: Modifying Elements

Mariana Hoffmann
PLUS
Mariana Hoffmann
Courses Plus Student 11,046 Points

New Items don't appear properly

Hey guys,

I can't find out where I'm missing something, but when I add a new item, the text doesn't appear, and you can't edit or something.

Here's what I've done.

// 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 elements need to modifying 
  checkBox.type = "checkbox";
  editInput.type = "text";

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

  label.innerText = taskString; 

  // each elements neet to 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("Some New Task");

  // Append listItem to incompleteTaskHolder
  incompleteTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskCompleted);
  }

// Edit an existing task
var editTask = function() {
  console.log("Edit Task...");
  //when the edit button is pressed
    //if the class of the parend is .editMode
      //switch from .editMode
      //label text becom the input's value
    //else
      //switch to .editMode
      //input value becomes the label's text

      // toggle .editMode on the parent
}

// 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 completed...");

  //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 task list list item to the #incomplete-tasks
  var listItem = this.parentNode;
  incompleteTasksHolder.appendChild(listItem);
  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 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 completedTasksHolder 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

Steven Parker
Steven Parker
231,248 Points

You aren't passing the text to the function that creates the new item.

In addTask, you have this code:

  //create a new list item with the text from #new-task
  var listItem = createNewTaskElement("Some New Task");

Despite the comment, you are just passing a fixed string as the name of the new task. You probably want something like this instead:

  var listItem = createNewTaskElement(taskInput.value);

And you won't be able to edit until the editTask function is implemented. Right now it's empty except for comments. As you continue in the course, the implementation of the edit function will be covered.