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

Shadrin Sergei
Shadrin Sergei
5,225 Points

I can't debug this code, it gives me back an error - Failed to execute 'appendChild' on 'Node': parameter 1 is not of ty

I can't debug this code, it gives me back an error - Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'. Please help me.

//Problem: User interaction doesn't provide desired results.
//Solution: Add iteractivity 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");
    //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";

        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);
    taskInput.value = "";
}
//Edit an existing task
var editTask = function() {
    console.log("Edit task");

    var listItem = this.parentNode;
    var editInput = list.querySelector("input[type=text]");
    var label = listItem.querySelector("label");

    var containsClass = listItem.classList.contains("editMode");
        //if the 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
        listItem.classList.toggle("editMode");
}

//Delete an existing task
var deleteTask = function() {
    console.log("task delete...");
    //Remove the parent list item from the ul
    var listItem = this.parentNode;
    var ul = listItem.parentNode;
    ul.removeChild(listItem);
}


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

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

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.deleted");
        //bind editTask to edit Button
        editButton.onclick = editTask;

        //bind deleteTask to delete button
        window.onload = function () {
        deleteButton.onclick = deleteTask;
        }

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

var ajaxRequest = function() {

    console.log("AJAX request");
}
//Set the click handler to the addTask function
addButton.addEventListener("click", addTask);
addButton.addEventListener("click", ajaxRequest);  

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


//cycle over incompleteTaskHolder ul list items
for(var i = 0; i < incompleteTasksHolder.children.length; i++) {
    //for each list item
        //bind events to list item's children (taskIncomplete)
        bindTaskEvents(completedTasksHolder.children[i], taskIncomplete());
}
geoffrey
geoffrey
28,736 Points

Please format your code with Markdown.

You just need in this case to surround your code with backticks and the name of the language, check the documentation, it's straightforward.

It's important to provide this little effort, it makes your code more readable and people will want to help you more easily.

Not sure if the documentation explicitly states it, but you also need a blank line before and after your code block (three backticks) to format it correctly.

2 Answers

Mark Casavantes
PLUS
Mark Casavantes
Courses Plus Student 13,401 Points

Hi Shadrin,

On line 2 should type be typeof?

In your second block of code. list is not defined. Maybe you meant to type dist, or you're using a variable you didn't define.

In your third block. window is not defined.

Correct me if I am wrong. I hope this is helpful.

type is fine, it's referring to the input element's type property/attribute.

list in the editTask function should be listItem. Is that what you meant?

window is defined in the browser context, and is a global variable, so it doesn't need to be defined (and shouldn't, since it already has a purpose).

Okay, so first up, fix list in the editTask function, should be listItem:

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

Second in your bindTaskEvents function, your deleteButton should be looking for the button with a class of delete, not deleted:

var deleteButton = taskListItem.querySelector("button.delete");

Third, you don't want to only bind deleteTask to the deleteButton(s) on the window load event, but every time the bindTaskEvents function is run, so remove the `window.onload' function wrapper:

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

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

And last, but definitely not least, your second loop is also looping over the incomplete tasks, not the completed tasks. Should be:

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