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

Javascript Basics, Stage 3, Traversing & Manipulating the DOM Error editButton not defined.

When I check my code in the browser at the end of the videos, I get the error editButton not defined. I have (I think) checked mine against the download files and can't find the error. the code for the script is:

//Problem: User interaction doesn't provide desired results
//Solution: Add interactivity so the user can mangage 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

// Add a new task
var addTask = function() {
  console.log("Add task...");
    //When the button is pressed 
    //Create a new list item with text from #new-task:
    //imput (checkbox)
    //label
    // input (test)
    //button.edit
    //button.delete
    //Each of these elements will need to be modified and appended

}

//Edit an existing task
var editTask = function() {
    console.log("Edit task...");
    //When edit button is pressed
    //if the class of the parent is .edit
    //switch from .editmode
    //label text became 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...");
    //when the delete button is pressed
   //remove the parent list item from the unordered list

}  

//Mark a task as complete. 
var taskCompleted = function() {
    console.log("Task complete...");
   //When the checkbox is checked
   //append the task  list item to the #completed-tasks
}

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

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

//cycle over completedTaskHolder 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);
}

2 Answers

Hi Sally,

You declared the variable ediButton inside a function, this means its scope is restricted to that function. You need to declare it on the top so its available everywhere.

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
var editButton  = document.createElement("button");

Actually, I did want it in the function. I had it outside the function, but not globally declared. Thanks for your answer. It helped me narrow down what I was looking for.