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 trialjessica grinberg
14,117 Pointslabel has a value of undefined
Hi, when I test the addTask function, it returned a task with a value of undefined, the add and delete work it's just it's value which is not returned properly and is still undefined. I have been trying to debug my code for a possible mistake but I dont see any. Here is the following code:
var taskInput = document.getElementById('new-task'); //new-task
var addButton = document.getElementsByTagName('button')[0]; //first button on page
var incompleteTasksHolder = document.getElementById('incomplete-tasks'); //incomplete-tasks
var completedTasksHolder = document.getElementById('completed-tasks'); //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";
editInput.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(taskInput){
console.log("Add task..");
var listItem = createNewTaskElement(taskInput.value);
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
};
var editTask = function(){
console.log("Edit task..");
};
var deleteTask = function(){
console.log("Delete task..");
var listItem = this.parentNode;
var ul = listItem.parentNode;
ul.removeChild(listItem);
};
var taskCompleted = function(){
console.log("Task Complete..");
var listItem = this.parentNode;
completedTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
};
var taskIncomplete = function(){
console.log("Task Incomplete..");
var listItem = this.parentNode;
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
};
var bindTaskEvents = function(taskListItem, checkBoxEventHandler){
console.log("Bind list items 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< incompleteTasksHolder.children.length ; i++) {
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
for (var i=0; i< completedTasksHolder.children.length ; i++) {
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}
1 Answer
jessica grinberg
14,117 PointsI found my mistake when I typed the question! I passed the argument taskInput to the addTask function.
Chris Freeman
Treehouse Moderator 68,457 PointsChris Freeman
Treehouse Moderator 68,457 PointsGreat job!