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

How can the taskListItem hold their binding outside the function scope?

Hi,

I am trying to understand:

1- how can editButton, deleteButton and checkBox binding be used outside the scope of the function they are in? since they have been declared within the anonymous function referenced to bindTaskEvents but still once the page load if I click a button or checkbox the console is logging the events.

2- Minor clarification required: In C# you can run for loop within a function, whereas in Javascript the for loop block can be on its own, is it correct?

Thanks.

2 Answers

I'm not 100% sure that I'll be able to explain this clearly, but I'll do my best.

It might be easier to think of it in terms of the DOM element that gets created. Let's take a look at an abbreviated version of the code using just the edit button:

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {  
  var editButton = taskListItem.querySelector("button.edit"); // edit button
  editButton.onclick = editTask; // bind to editTask function
}

You end up with an edit button that has the onclick event bound to the editTask() function. So the DOM element could look like:

<button class="edit" onchange="editTask()">Edit</button>

So it's not so much that they're being called out of scope. They're associated with the element.

It's not a perfect example, but say you changed the CSS of the element instead:

var bindTaskEvents = function(taskListItem, checkBoxEventHandler) {  
  var editButton = taskListItem.querySelector("button.edit"); // edit button
  editButton.style.background = blue; // change the background to blue
}

Those styles would still apply even outside the scope of that function, right? It's similar.

If it would help, you can actually look at the DOM properties of an element using the web inspector in most browsers. In Firefox for example, you can right click on an element on the page and do Inspect Element. Then right click on the corresponding HTML and click Show DOM Properties. You can then view all of the properties for that element, including onclick.

Hopefully that makes sense, but let me know if you have any questions.

In answer to your other question, yes you can have for loops outside of a function.

// For each list item in the completed items <ul>, taskIncomplete().
for(var i = 0; i < completeTaskHolder.children.length; i++) {
  bindTaskEvents(completeTaskHolder.children[i], taskIncomplete);
}

This is run when the script is originally referenced in the HTML, so we can bind all of the elements that already exist on the page.

Happy coding!

Hi Rydavim,

Thank you for the thorough reply. Your answers were crystal clear. Both points make sense now.

Regards, Sami