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
Brian Mendez
7,734 PointslistItem is not defined. Code not working. Pls help.
Here is my code. I keep getting 'listItem is not defined'. Help is appreciated.
var taskInput = document.getElementById("new-task");
var addButton = document.getElementsByTagName("button")[0];
var incompleteTasksHolder = document.getElementById("incomplete-tasks");
var completedTasks = document.getElementById("completed-tasks");
var createNewTaskElement = function(taskString) {
var listItem = document.createElement("li");
var checkBox = document.createElement("input");
var label = document.createElement("label");
var editInput = document.createElement("input");
var editButton = document.createElement("button");
var deleteButton = document.createElement("button");
checkBox.type = "checkbox";
checkBox.type = "text";
editButton.innerText = "Edit";
editButton.className = "edit";
deleteButton.innerText = "Delete";
deleteButton.className = "delete";
label.innerText = taskString;
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
return listItem;
}
var addTask = function() {
console.log("Add Task..");
var listItem = createNewTaskElement("Some New Task");
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
var editTask = function() {
console.log("Edit Task..");
}
var deleteTask = function() {
console.log("Delete Task..");
}
var taskCompleted = function() {
console.log("Task Completed");
completedTasks.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
}
var taskIncomplete = function() {
console.log("Task Incomplete..");
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
console.log("bind list item events");
var checkBox = taskListItem.querySelector("input[type=checkbox]");
var editButton = taskListItem.querySelector("button.edit");
var deleteButton = taskListItem.querySelector("button.delete");;
editButton.onclick = editTask;
deleteButton.onclick = deleteTask;
checkBox.onchange = checkBoxEventHandler;
}
addButton.onclick = addTask;
for (var i = 0; i < completedTasks.children.length; i++) {
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
for (var i = 0; i < completedTasks.children.length; i++) {
bindTaskEvents(completedTasks.children[i], taskIncomplete);
}
1 Answer
jobbol
Full Stack JavaScript Techdegree Student 19,117 PointsInside the functions: taskCompleted and taskIncomplete you reference listItem, but it does not exist within the scope of those functions.
var taskCompleted = function () {
console.log("Task Completed");
completedTasks.appendChild(listItem); //referenced listItem, but it's not in this scope.
bindTaskEvents(listItem, taskIncomplete);
}
var taskIncomplete = function () {
console.log("Task Incomplete..");
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
Either pass listItem to your functions or declare it from inside.
This is from a code project within one of the javascript courses right? I have a feeling you may have skipped a step somewhere.