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 AJAX Basics (retiring) jQuery and AJAX Stage 3 Challenge

Zak Posner
Zak Posner
8,473 Points

About the use of the .each() method in this task

I'm confused about the way the .each() method is used in this lesson. The following is provided as example code for us to build off:

$(document).ready(function () {
  $.getJSON('../data/employees.json', function (data) {
    var statusHTML = '<ul class="bulleted">';
    $.each(data,function (index, employee) {
      if (employee.inoffice === true) {
        statusHTML +='<li class="in">';
      } else {
        statusHTML +='<li class="out">';
      }
      statusHTML += employee.name + '</li>';
    });
    statusHTML += '</ul>';
    $('#employeeList').html(statusHTML)
  });

Im confused as how how the .each() method is going over the results of the JSON file when it is attached to the JQuery object. It seems like the function is operating on the first argument (data) but I checked the JQuery documentation and can't find any similar examples.

I thought the .each() method only worked on the object it is attached to?

1 Answer

There's a difference: For this situation: http://api.jquery.com/jquery.each/

For other situations: https://api.jquery.com/each/

It all depends on what you're doing. In this case you are calling jquery.each aka $.each(array, callback(index, value)).

The other .each method is typically used on jQuery objects like $("li") objects, for example.