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) Programming AJAX Stage 2 Challenge

Chris Reich
Chris Reich
15,163 Points

What does roomstatusHTML += '<li class="empty">; do?

What is the relationship between the name of the ul class and the var roomstatusHTML? I guess my question is, does the name of the ul classs have something to do with the JSON data or is it arbitrary? Code below.

var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if(xhr.readyState === 4 && xhr.status === 200) { var employees = JSON.parse(xhr.responseText); var statusHTML = '<ul class="bulleted">'; for (var i=0; i<employees.length; i += 1) { if (employees[i].inoffice === true) { statusHTML += '<li class="in">'; } else { statusHTML += '<li class="out">'; } statusHTML += employees[i].name; statusHTML += '</li>'; } statusHTML += '</ul>'; document.getElementById('employeeList').innerHTML = statusHTML; } }; xhr.open('GET', '../data/employees.json'); xhr.send();

var xhrr = new XMLHttpRequest(); xhrr.onreadystatechange = function () { if(xhrr.readyState === 4 && xhr.status === 200) { var rooms = JSON.parse(xhrr.responseText); var roomstatusHTML = '<ul class="rooms">'; for (var i = 0; i<rooms.length; i+=1) { if (rooms[i].available === true) { roomstatusHTML += '<li class="empty">'; } else { roomstatusHTML += '<li class="full">';
} roomstatusHTML += rooms[i].room; roomstatusHTML += '</li>'; } roomstatusHTML += '</ul>'; document.getElementById('roomList').innerHTML = roomstatusHTML; } }; xhrr.open('GET', '../data/rooms.json'); xhrr.send();

2 Answers

John Coolidge
John Coolidge
12,614 Points


roomstatusHTML (or statusHTML, as it appears in your code) followed by += takes the value of statusHTML and adds to it the string after the +=, in this case '<li class="empty">'. Whatever the value of statusHTML, the string '<li class="empty">' will be added to it.

The class name of the ul (or do you mean the li in your question header?) is added so that CSS can be applied to that element. If you add the li to the statusHTML variable without the class 'empty', the CSS in the CSS file under '.empty' will not be applied to that li when it is displayed on the web page. It has nothing to do with JSON nor is it arbitrary.

I hope this helps, John

In JavaScript += simply means to add something to the current value. For a string, you are adding extra words or characters onto that string.

In this case, it will add the string '<li class="empty">' onto the end of the variable roomstatusHTML, creating a larger string. This was you can quickly iterate over and over, adding the html needed for each room in the array.

The class name is for the styling of the list item. If you check out the 'css/main.css' file in workspaces, it contains the styling for these list items. The class has nothing to do with JavaScript or JSON, it's simply to visually show whether the person is in the office or not, or whatever.