Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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:)