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 trialmironpingel
Courses Plus Student 599 PointsEdit Button is not working.. And how could it?..
When i click the edit button nothing happens. And I get that, since the editTask function is never called.. What I dont get is why it works in the video. On the addTask button we set an onclick event to trigger related function, but we never did that with the Edit and Delete buttons. Can anyone explain this to me? And I have compared my code and the downloadable code several times but I simply cant figure out what makes the button work.. :(
Here is my code:
//Problem: User interaction doesn't provide desired results.
//Solution: Add interactivty so the user can manage 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
// New task list item
var createNewTaskElement = function(taskString){
// Create list item
var listItem = document.createElement("li");
//input (checkbox)
var checkBox = document.createElement("input");
//label
var label = document.createElement("label");
//input (text)
var editInput = document.createElement("input");
//button.edit
var editButton = document.createElement("button");
//button.delete
var deleteButton = document.createElement("button");
//Each elements, needs modified
checkBox.type = "checkbox";
editInput.type = "text";
label.innerHTML = taskString;
editButton.className = "edit";
editButton.innerHTML = "Edit";
deleteButton.className = "delete";
deleteButton.innerHTML = "Delete";
//and appended
listItem.appendChild(checkBox);
listItem.appendChild(label);
listItem.appendChild(editInput);
listItem.appendChild(editButton);
listItem.appendChild(deleteButton);
return listItem;
};
//Add a new task
var addTask = function() {
console.log("Add task...");
//Create a new list item with the text from #new-task:
var listItem = createNewTaskElement(taskInput.value);
// Append List item to incompleteTasksHolder
incompleteTasksHolder.appendChild(listItem);
}
//Edit an existing task
var editTask = function() {
console.log("Edit task...");
var listItem = this.parentNode;
var editInput = listItem.querySelector("input[type=text");
var label = listItem.querySelector("label");
var containsClass = listItem.classList.contains("editMode");
//if the class of the parent is .editMode
if(containsClass) {
//Switch from .editMode
//label text become the input's value
label.innerText = editInput.value;
} else {
//Switch to .editMode
//input value becomes the label's text
editInput.value = label.innerText;
}
//Toggle .editMode on the list item
listItem.classList.toggle("editMode");
};
//Delete an existing task
var deleteTask = function() {
console.log("Delete task...");
//When the Delete button is pressed
//Remove the parent list item from the ul
}
//Mark a task as complete
var taskCompleted = function() {
console.log("Task complete...");
//Append the task list item to the #completed-tasks
var listItem = this.parentNode;
completedTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
}
//Mark a task as incomplete
var taskIncomplete = function() {
console.log("Task incomplete...");
//Append the task list item to the #incomplete-tasks
var listItem = this.parentNode;
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
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 edit button
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 li items
for(var i = 0; i < incompleteTasksHolder.children.length; i++) {
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
};
// for each list item
// bind events to list items children ( taskCompleted)
// cycle over completeTaskHolder ul li items
for(var i = 0; i < completedTasksHolder.children.length; i++) {
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
};
// for each list item
// bind events to list items children ( taskIncomplete)
Thanks for the help :))
mironpingel
Courses Plus Student 599 PointsThanks I was wondering how to do that :D
6 Answers
Chyno Deluxe
16,936 PointsTaking a quick look at the code I see a typo.
var editInput = listItem.querySelector("input[type=text"); //You are missing the closing "]" after type=text
//change to
var editInput = listItem.querySelector("input[type=text]");
David Dickerson
13,845 PointsHey Miron,
I think I have an answer to your question about how the 'delete' and 'edit' buttons work. The way you declared the functions may have something to do with it. For example you usually declare a function like this...
function print(message) { document.write(message) }
But in this course he had us write the functions like this...
var print = function(message) { document.write(message) }
From what I can I tell I think that made a difference so that you didn't have to call the function to have it executed. Hope this helps.
mironpingel
Courses Plus Student 599 PointsYes, thank you both :) It works now , and i figured it out :)
Karen Rulander
20,520 Pointsthe edit button works because of the 'toggle' method.
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 Points@Miron Pingel where is the issue I have code exactly like yours and I can't catch the bug. Was it the missing closing ]? I also fixed that but nothing is happening can't edit added tasks or remove them as completed.
Chyno Deluxe
16,936 PointsHey Christopher,
Can you please post your code. I know you mentioned that its the exact same but just to be sure, you should post it and we can all take a look.
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 PointsHi Chyno, thanks I downloaded course code and caught my little error
Samuel Webb
25,370 PointsSamuel Webb
25,370 PointsJust made the code easier to read by adding the word
javascript
after the first 3 back ticks. That's how you get colors by the way.