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
Colin Kearns
6,135 PointsBinding on a loop, what am I doing wrong?
//Problem: User interaction does't provide desired results.
//Solution: Add interactivity 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("complete-tasks"); //completed-tasks
//Add a new task
var addTask = function () {
console.log('Add task...');
//When the button is pressed
//Create a new list item with the text from #new-task:
//input (checkbox)
//label
//input (text)
//button.edit
//button.delete
//Each elements, needs modified and appended
}
//Edit an existing 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
//else
//Switch to .editMode
//input value becomes the label's text
//Toggle .editMode on the parent
}
//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...');
//When the checkbox is checked
//Append the list item to the #completed-tasks
}
//Mark a task as incomplete
var taskIncomplete = function() {
console.log('Task incomplete...');
//When the checkbox is unchecked append the list item to the #incomplete-tasks
}
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.onlick = 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 list items
for(var i = 0; 1 < incompleteTasksHolder.children.length; i++) {
//bind events to list item's children (taskCompleted)
bindTaskEvents(incompleteTasksHolder.children[1], taskCompleted);
}
//cycle over completeTaskHolder ul list items
for(var i = 0; 1 < completedTasksHolder.children.length; i++) {
//bind events to list item's children (taskIncompleted)
bindTaskEvents(completedTasksHolder.children[1], taskIncomplete);
}
2 Answers
Aaron Graham
18,033 PointsWithout knowing exactly what the problem you are experiencing is, I would say that you might want to look at your loops. They only seem to be assigning running bindTaskEvents() on one child element. Try referencing the child element using your counter variable in you loops. Like this:
bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted
Basically, just change the 1 to an i.
There are also forEach array methods that might be more suitable. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
Edit: Actually, scratch that. Your child elements won't have forEach methods.
Colin Kearns
6,135 PointsThanks guys, this solved my endless binding problem. I am not even sure what the iterator variable is honestly, this app is going way over my head. I think I am going to need to incorporate some outside resources in my Javascript learning, the basics classes made sense to me, but I am pretty lost at this point.
Iain Simmons
Treehouse Moderator 32,305 PointsIain Simmons
Treehouse Moderator 32,305 PointsThe
1 < incompleteTasksHolder.children.lengthand1 < completedTasksHolder.children.lengthparts of the loops also need to be changed to use the iterator variablei.Aaron Graham
18,033 PointsAaron Graham
18,033 PointsIain Simmons - Good catch. You're right.