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
Jonathan Grieve
Treehouse Moderator 91,254 PointsaddTask does not work properly - Help :D
Hello you fine people of Treehouse Island. I bow to your superior knowledge once again. ;)
I'm creeping ever so slowly through the Interactive Web Pages with Javascript course. As far as I'm aware I've written the same code Andrew has. I'm up to the part where he tests the code is right after adding a task to the list.
//PLANNING:
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 createNewTaskElement = function(taskString) {
//create List Item
var listItem = document.createElement("li");
//input checkbox
var checkBox = document.createElement("input");
//input label
var label = document.createElement("label");
//input[text]
var editInput = document.createElement("input");
//input[text]
var editButton = document.createElement("button");
//button .delete
var deleteButton = document.createElement("button");
//Each element modifying
checkBox.type = "checkbox";
editInput.type = "text";
editButton.innerHTML = "Edit";
editButton.className = "delete";
deleteButton.innerHTML = "Delete";
deleteButton.className = "delete";
label.innerText = taskString;
//Each Element needs appending
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
return listItem;
}
var addTask = function() {
console.log("Add Task...");
//#new-task
//when button is pressed
//create a task
//(create a new list item with the text from #new-task.
var listItem = createNewTaskElement(taskInput.value);
//Append listItem to incompleteTaskHolder
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
//Edit an exisiting 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
//switch to .editMode
//input value becomes the label's text. //toggle .editMode
}
//Delete an existing task.
var deleteTask = function() {
console.log("Delete Task...");
//when the delete button is pressed
//remove the parent list item (li)
var listItem = this.parentNode;
var ul = listItem.parentNode;
ul.removeChild(listItem);
}
var taskCompleted = function() {
console.log("Completed Task...");
//Mark a task as complete.
//append the task list item to the #completed-tasks
var listItem = this.parentNode;
completedTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
}
var taskIncomplete = function() {
console.log("Incompleted Tasks. Task...");
//Mark a task as incomplete.
//when the checkbox is unchecked
//append the task to incomplete tasks.
var listItem = this.parentNode;
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
//Set the click handler to the addTask Handler.
addButton.onclick = addTask;
var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
console.log("bind list items...");
//select list items 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 the taskCompleted to the checkbox
checkbox.onchange = checkBoxEventHandler;
/**/
}
//Cycle over the incompletesTaskHolder ul list items
for (var i=0; i < incompleteTasksHolder.children.length; i++) {
//bind events to list items children(taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
//Cycle over the completeTasksHolder ul list items
//for each li
for (var i=0; i < completedTasksHolder.children.length; i++) {
// bind events to list items children (taskIncomplete)
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}
//Not about minimum amounts of code but clarity
//removeChild is opposite to append();
The above code gives the output
Uncaught TypeError: Cannot set property 'onclick' of null app.js:120
bindTaskEvents app.js:120
addTask app.js:55
I've had a good go but I can't seem to get to the bottom of this.
1 Answer
Ken Alger
Treehouse TeacherJohnathan;
Your code is:
//Each element modifying
checkBox.type = "checkbox";
editInput.type = "text";
editButton.innerHTML = "Edit";
editButton.className = "delete";
deleteButton.innerHTML = "Delete";
deleteButton.className = "delete";
label.innerText = taskString;
The editButton.className should equal "edit" not "delete"
With that change the errors stopped.
Happy coding,
Ken
Jonathan Grieve
Treehouse Moderator 91,254 PointsJonathan Grieve
Treehouse Moderator 91,254 PointsYay! It did it! Why did I not pick this up this time yesterday?
I swear I looked at every last line of code lol
Thank you!