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
Joshua David
5,494 PointsHaving trouble getting the text entered to appear when I click add task
I have reviewed the code and can't see to find my issue. When I click add task, the text doesn't show up as in the video. I am using textContent instead of innerHtml because I am using Firefox. Any help would be greatly appreciated.
// Problem: User interaction doesn't provide desired results
// Solution: Add interactivity, so the user can manage daily tasks.
// < -------------- Plan -------------- >
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"); // Type is checkbox
// label
var label = document.createElement("label");
// input (text)
var editInput = document.createElement("input");
// button with a class and the text of edit
var editButton = document.createElement("button");
// button with a class and the text of delete
var deleteButton = document.createElement("button");
// Each element needs modifying
checkBox.type = "checkbox";
editInput.type = "text";
editButton.textContent = "Edit";
editButton.className = "edit";
deleteButton.textContent = "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;
}
/* When the user clicks on the add task button: it will createNewTaskElement with all of the complexity in the fuction, then return that item back into the variable listItem in the addtask function then append it to the incompleteTasksHolder */
// 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 incompleteTasksHolder
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
// Edit an existing task
var editTask = function() {
console.log("Edit task...");
/* When the Edit button is pressed:
If the class of the parent (li) is editMode,
Switch from .editMode and make the label text
become the inputs' value
Else
switch to .editMode
and the input value becomes the label's text
Toggle .editMode on the parent (li)
*/
}
// Delete an existing task
var deleteTask = function() {
console.log("Delete task...");
var listItem = this.parentNode;
var ul = listItem.parentNode;
// Remove the parent (li) from the unordered list
ul.removeChild(listItem);
}
// Mark a task as complete
var taskCompleted = function() {
console.log("Task completed...");
//Append the task (li) to the #complete-tasks id
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 (li) to the #incomplete-tasks id
var listItem = this.parentNode;
incompleteTasksHolder.appendChild(listItem);
bindTaskEvents(listItem, taskCompleted);
}
// < -------------- Perform -------------- >
// Function that will bind the task events and select the li
var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
console.log("Bind list item evevnts");
//select taskListItem's children (child of ul = li)
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 the deleteTask to the delete checkBox
deleteButton.onclick = deleteTask;
// bind the checkBoxEventHandler to the checkbox
checkBox.onchange = checkBoxEventHandler;
}
// Set the click handler to the addTask function
addButton.onclick = addTask;
// cycle over the incompleteTasksHolder ul list items
for (var i = 0; i < incompleteTasksHolder.children.length; i++) {
// bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}
// cycle over the completeTasksHolder ul list items
for (var i = 0; i < completedTasksHolder.children.length; i++) {
// bind events to list item's children (taskIncomplete)addButton
bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}
1 Answer
Joshua David
5,494 PointsNever mind, I figured it out. I forgot to set my "label.innerText" to "Label.textContent :)