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

Why another each() method (which we were told about in "jQuery Basics" doesn't work here?

The console tells me: Uncaught TypeError: $employees.each is not a function. What's wrong with my code?

$(document).ready(function () {
    $.getJSON('data/employees.json', function(response){
        var $ul = $("<ul class='bulleted'></ul>");
        var $employees = response;
        $employees.each(function(){
            var $li = $('<li></li>');
            $li.text($(this).name);
            if ( $(this).inoffice){
                $li.addClass('in');
            }else{
                $li.addClass('out');
            };
            $ul.append($li);
        });
        $('#employeeList').append($ul);
    });
}) // end read

2 Answers

Emmanuel Molina
Emmanuel Molina
9,268 Points

Because $employees.each(function(){}); isn't the correct synthax :) Try $.each($employees, function(){}); instead.

Justin Roberts
Justin Roberts
14,583 Points

Try logging the response. One thing I think it might be is that the response is not a jQuery object (which it has to be to use a jQuery function). JavaScript itself does have an each type function, it's just "forEach" rather than just "each".