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 trialGarrett Sanderson
12,735 PointsWhen I try to create a new task the value is output as taskString and I don't know why.
// Problem: User Interaction doesn't provide desired results.
// Solution: Add interactivity so the user can manage daily tasks.
var taskInput = document.getElementById("new-task"); //news-task
var addButton = document.getElementsByTagName("button")[0]; //first-button
var incompleteTaskHolder = document.getElementById("incomplete-tasks"); //incomplete-tasks
var completedTaskHolder = document.getElementById("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"); //checkbox
// Label
var label = document.createElement("label");
// Input (text)
var editInput = document.createElement("input"); //text
// Button.edit
var editButton = document.createElement("button");
// Button.delete
var deleteButton = document.createElement("button");
// Each elements needs modifiying.
checkBox.type= "checkbox";
editInput.type = "text";
editButton.innerText = "Edit";
editButton.className = "edit"
deleteButton.innerText = "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;
}
// 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 listItem to incompleteTaskHolder
incompleteTaskHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
// Edit an existing taks.
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 form .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...");
var listItem = this.parentNode;
var ul = listItem.parentNode;
// Remove the parent list item from the ul
ul.removeChild(listItem);
}
// 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
completedTaskHolder.appendChild(listItem);
bindTaskEvents(listItem, taskIncomplete);
}
// Mark a task as incomplete.
var taskIncomplete = function() {
console.log("Task Incomplete...");
//Append the list item to the #incomplete-tasks
var listItem = this.parentNode
incompleteTaskHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
console.log("Bind list item events");
// Select it's children
// bind the editTask to edit button
// bind the deleteTask to the delete button
// bind checkBoxEventHandler to the checkbox
}
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;
}
// Set the click handler to the addTask function
addButton.onclick = addTask;
// cycle over incompleteTaskHolder ul list items
for(var i = 0; i < incompleteTaskHolder.children.length; i++) {
// Bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTaskHolder.children[i], taskCompleted);
}
// cycle over completeTaskHolder ul list items
for(var i = 0; i < completedTaskHolder.children.length; i++) {
// Bind events to list item's children (taskIncomplete)
bindTaskEvents(completedTaskHolder.children[i], taskIncomplete);
}
Can someone help me figure out why?
1 Answer
James Best
11,112 PointsHey Garrett,
From looking at your code, you are currently outputting the string "taskString" rather than the variable taskString. If you remove the quotes surrounding the variable then it should work.
I am pretty sure I did the same thing.
I hope this helps.
James
Garrett Sanderson
12,735 PointsGarrett Sanderson
12,735 PointsThanks James!
Totally missed that.
James Best
11,112 PointsJames Best
11,112 PointsNo worries, glad I could help.