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 The Office Status Project Revisited

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

i preferred this way but i got a question! is it something to do with closure? or let function level scoping?

$(document).ready(function(){
    let url = 'data/employees.json';
    let callback = function(response){
        let employeeStatus = '<ul class="bulleted">';
        $.each(response, function(index,value){

            if(value.inoffice === true){
                employeeStatus += '<li class="in">';
            } else {
                employeeStatus += '<li class="out">';
            }
            employeeStatus += value.name + '</li>';
        });
        employeeStatus += '</ul>';
        $('#employeeList').html(employeeStatus);
    };
    $.getJSON(url, callback);
});  //ends ready

when i move this>>>

 let employeeStatus = '<ul class="bulleted">';

to here inside each method, call back function like this>>>>

$(document).ready(function(){
    let url = 'data/employees.json';
    let callback = function(response){

        $.each(response, function(index,value){
        let employeeStatus = '<ul class="bulleted">';  
            if(value.inoffice === true){
                employeeStatus += '<li class="in">';
            } else {
                employeeStatus += '<li class="out">';
            }
            employeeStatus += value.name + '</li>';
        });
        employeeStatus += '</ul>';
        $('#employeeList').html(employeeStatus);
    };
    $.getJSON(url, callback);
});  //ends ready

the second won't work as like it won't log the employee list

1 Answer

Gabbie Metheny
Gabbie Metheny
33,778 Points

When you move your let employeeStatus = '<ul class="bulleted">' inside your $.each function, you're adding new opening <ul> tags with each loop through the employee list. You need to add your opening ul tags before beginning to loop through the list, and add the closing ul tags after finishing the loop, in order for your list to render properly.

Saqib Ishfaq
Saqib Ishfaq
13,912 Points

Gabbie Metheny Thanks! so simple yet important to know the basics... appreciate the insight:)