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 trialEric Welch
20,754 PointsbindTaskEvents not logging to console/ button errors
My binding efforts are failing to log to console. I've ran my .js through a couple of checkers and couldn't find any syntax errors and I think I caught all the task/tasks mistypes.
I've reviewed the course through this point and can't see where I went wrong.
I tried pasting this code into console:
editButton.onclick = editTask;
Which returns an uncaught reference error: editButton undefined
I'm confused
Here's my code in entirety:
//problem:user interaction doesn't provide desired results
//solution: add interactivity
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 new task
var addTask = function () {
console.log("Add task...");
//when button is pressed, create new list item with text from #new_task:
//input (checkbox)
//label
//input (text)
//button.edit
//button.delete
//each elements, need to be modified and appended
}
//edit existing task
var editTask = function () {
console.log("Edit task...");
//when edit button is pressed
//if class of parent is .editmode
//switch from .editMode
//label text become the input's value
//else
//switch to .editMode
//input value becomes the label's text
//toggle .editMode on the parent
}
//delete existing task
var deleteTask = function() {
console.log("Add delete...");
//when the delete button is pressed
//remove the parent list item from the ul
}
//mark task completed
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 #completed-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");
//Set the click handler to the addTask function
addButton.onclick = addTask;
//bind editTask to edit button
editButton.onclick = editTask;
//bind deleteTask to delete button
deleteButton.onclick = deleteTask;
//bind checkBoxEventHandler to checkbox
checkBox.onchange = checkBoxEventHandler;
// cycle over incompleteTaskHolder ul lis
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
//bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
};
// cycle over completedTaskHolder ul lis
for (var i = 0; i < completedTasksHolder.children.length; i++) {
//bind events to list item's children (taskIncomplete)
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
};
}
2 Answers
Garrett Sanderson
12,735 PointsHi Eric,
You forgot to close your function body.
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 the editTask to edit button
editButton.onclick = editTask;
// bind the deleteTask to the delete button
deleteButton.onclick = deleteTask;
// bind taskCompleted to checkbox
checkBox.onchange = checkBoxEventHandler;
} /* <----- curly bracket here.
Let me know if this helps!
Eric Welch
20,754 PointsAh, yes. It became obvious once pointed out. Thank you