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

Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.app.js:51 addTask

Interactive Web Pages with Javascript.

Code produces a failed to execute error on line 51.

See below:

//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"); //label
    //input text
    var editInput = document.createElement("input"); //text
    //button.edit
    var editButton = document.createElement("button"); //edit
    //buton.delete
    var deleteButton = document.createElement("button"); //delete
    //Each element needs to be modified

  checkBox.type = "checkbox";
  editInput.type = "text";

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



    //Each element needs to be appended
  listItem.appendChild(checkBox);
  listItem.appendChild(label);
  listItem.appendChild(editInput);
  listItem.appendChild(editButton);
  listItem.appendChild(deleteButton);
}

//Add a new task
var addTask = function(){
  console.log("Add task...");

  //When the button is pressed
  //Create a new list item with the text from the #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...");
  //When the edit button is pressed
    //If the class of the parent is .editMode
      //Switch from .editMode
      //label text become the input's value
    //else
      //Switch to .editMode
      //input value becomes to label's text
    //Toggle .editMode
}
//Delete an existing task
var deleteTask = function(){
    console.log("Delete task...");
  //When the delete button is pressed
  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("Task incomplete...");
  //Append the task list item to the #incomplete-tasks
  var listItem = this.parentNode;
  incompleteTasksHolder.appendChild(listItem);
  bindTaskEvents(listItem, taskCompleted);
}


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

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 taskCompleted to checkbox
  checkbox.onchange = checkboxEventHandler;
}

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

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

2 Answers

Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

createNewTaskElement() doesn't return the listItem it creates. Just add 'return listItem;' to the end of that function, and it should work.

This looks good but can you explain why this is happening?

Nicholas Olsen
seal-mask
.a{fill-rule:evenodd;}techdegree
Nicholas Olsen
Front End Web Development Techdegree Student 19,342 Points

Sure, so there are basically two types of functions: functions that spit out a value, and functions that don't.

function does_not_spit_out_a_value() {
    console.log("I don't spit out a value.");
}

function does_spit_out_a_value() {
    console.log("I do spit out a value");
    return "This is a value";
}

A value can be any data type, in the case above, it is a string. But it can be a number, an object, or array too. It can even be "null" or "undefined." When you try to assign the result of a function to a variable like this:

var foo = does_not_spit_out_a_value();

then the variable will hold a special value called "undefined" which really represents nothing. It doesn't have any properites. So when you try to call "appendChild()" on a variable that hold "undefined," you get an error.

On the other hand, if you did something like this:

var bar = does_spit_out_a_value();

Then you can do special things with the 'bar' variable because now instead of holding undefined, it holds a string.

Does that make sense?