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

Alex Flores
Alex Flores
7,864 Points

JavaScript Data Structure / Interpreter question?

I'm just curious if someone could tell me what the interpreter is doing in the following code and/or is there a name for this technique. The code below is basically a binding event:

<script>
//Create binding events so tasks can move freely
var bindTaskEvent = function(taskListItem, checkBoxEventHandler) {
  var deleteButton = taskListItem.querySelector("button.delete");
  var editButton = taskListItem.querySelector("button.edit");
  var checkBox = taskListItem.querySelector("input[type=checkbox]");

  //delete
  deleteButton.onclick = deleteTask;

  //edit
  editButton.onclick = editTask;

  //checkbox
  checkBox.onchange = checkBoxEventHandler;
}

addButton.onclick = addTask;

for(i = 0; i < incompletedTasks.children.length; i++){
  bindTaskEvent(incompletedTasks.children[i], taskComplete);
}

for(i = 0; i < completedTasks.children.length; i++){
  bindTaskEvent(completedTasks.children[i], taskIncomplete);
}
</script>

I understand how it works for the most part, but I'm not entirely sure if the interpreter runs the code and stores an instance of it (if so where?) or it just doesn't run it and once someone clicks on the button, then the interpreter runs to that line of code?

Thanks!

1 Answer

Alex Flores
Alex Flores
7,864 Points

I think I got it. It's called an event listener?