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

Devorah Brinson
Devorah Brinson
7,490 Points

No input goes over to the Incomplete tasks

When I add something to the todo list I only get the checkbox, edit, and delete buttons. No input comes over and I'm using Chrome. I did try to change the .value to textContent but that didn't make it work either.

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"); //completeds-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");
    //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 li with the text from #new-task:
    var listItem = createNewTaskElement(taskInput.value);

    //Append listItem to incompleteTasksHolder
    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 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 parent
    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("Complete task...");

    //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("Incomplete task...");
    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 delteTask to delete button
    deleteButton.onclick = deleteTask;
    //bind taskCompleted 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++) { //for each list item
    //bind events to list items children (taskCompleted)
    bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}

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

1 Answer

Kevin Kenger
Kevin Kenger
32,834 Points

Hey Devorah,

You're missing a single line of code inside of your createNewTaskElement function:

label.textContent = taskString;

taskString is the parameter passed to the function, as in var createNewTaskElement = function(taskString), and then it's referenced later inside of the addTask function, where it's passed the value of taskInput.value.

Ultimately, the function, with the addition of the new line of code should look something like this:

var createNewTaskElement = function (taskString) {
    //Create List Item
    var listItem = document.createElement("li");
    //input (checkbox)
    var checkBox = document.createElement("input"); //checkbox

    var label = document.createElement("label"); //label

    var editInput = document.createElement("input"); //input (text)

    var editButton = document.createElement("button"); //button.edit

    var deleteButton = document.createElement("button"); //button.delete

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

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

    label.textContent = taskString; // HERE'S THE NEW LINE

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

    return listItem;
};
Devorah Brinson
Devorah Brinson
7,490 Points

Thank you that worked! I knew I was missing the link here and couldnt see it.