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

Hayden Bradfield
Hayden Bradfield
1,797 Points

Are there any syntax errors with this JS code?

I am having difficulties following along in this JavaScript interactivity lesson. Has anyone already done this lesson before and can provide some useful insight? My head is blown.

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

    //Each element needs and 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 = createNewTaskElements("Some New Task");

  //Append listItem to IncompleteTasksHolder
  incompleteTasksHolder.appendChild(listItem);




}

//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 label's text

      //Toggle .editMode on the parent 

}

//Delete an existing task
var deleteTask = function() {
  console.log("Delete task...");
    //when the delete button  is pressed
      //remove parent list item from the ul

}

//Mark a task as complete
var completeTask = function() {
  console.log("Complete task...");
    //When the checkbox is checked
      //Append the task list item to the completed-tasks

}


//Mark a task as incomplete
var incompleteTask = function() {
  console.log("Task Incomplete...");
    //When the checkbox is unchecked
      //Append the list item to incomplete-tasks

}  

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  //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 the incompleteTasksHolder ul list items
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
      //bind events to the list item's children (taskCompleted)
  bindTaskEvents(incompleteTaskHolder.children[i], taskCompleted);
}


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

[mod edit - code formatting. You can display a code block by surrounding it with lines of 3 backticks ```, above and below]

1 Answer

Seth Kroger
Seth Kroger
56,413 Points

When you define the functions for toggling the complete checkbox you name them completeTask/incompleteTask but but you refer to them as taskCompleted/tasksincomplete, which don't exist, when you bind them as the bottom.