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

JavaScript Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Perform: Traversing Elements with children

Dmitri Andronik
Dmitri Andronik
4,182 Points

I dont understand the part when he is creating for loop,please explain someone

I dont understand what is he doing for that for loop,could someone explain me detaily please

var taskInput = document.getElementById("new-task");
var addButton = document.getElementsByTagName("button")[0];
var incompleteTasksHolder = document.getElementById("incomplete-tasks");
var completedTasksHolder = document.getElementById("completed-tasks");

var addTask = function() {
  console.log("add task");
}
var editTask = function() {
  console.log("edit task");
}
var deleteTaks = function() {
  console.log("delete task");
}
var taskCompleted = function() {
  console.log("complete task");
}
var taskIncomplete = function() {
  console.log("incomplete task");
}


var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {
  console.log("Bind list item events");
}
//
addButton.onclick = addTask;
//

for(var i = 0;i < incompleteTasksHolder.children.length; i++) {
  bindTaskEvents(incompleteTasksHolder.children[i], taskCompleted);
}

for(var i = 0;i < completedTasksHolder.children.length; i++) {
  bindTaskEvents(completedTasksHolder.children[i], taskIncomplete);
}

1 Answer

What the loop is doing is checking how many children elements are inside of the #incomplete-tasks element, and then for each one the code passes that child element as an argument into the bindTaskEvents function along with the taskCompleted callback function. Something similar is done for the child elements of #completed-tasks.

Since bindTaskEvents only logs a message, the end result will just be that message in the console for every element looped over.