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 Answer

Sergio Cruz
Sergio Cruz
15,550 Points

Callback function for roomRequest is creating empy list items in between the correct ones

The callback function is getting all the correct data and adding it to the roomList div. However, it is also creating empy list items and I can't figure out why. The roomList div in the index.html is empty. Heres my code:

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 xhr2 = new XMLHttpRequest();
xhr2.onreadystatechange = function (){
  if(xhr2.readyState === 4 && xhr2.status === 200){
    var rooms = JSON.parse(xhr2.responseText);
    var ulList = '<ul class="rooms">';
    for (var i=0 ; i<rooms.length ; i ++){
      if (rooms[i].available === true){
        ulList += '<li class="empty">';
      } else {
        ulList += '<li class="full">';
      }
      ulList += rooms[i].room;
      ulList += '<li/>';
    }
    ulList += '</ul>'
    document.getElementById('roomList').innerHTML = ulList;
  }
};
xhr2.open('GET', '../data/rooms.json');
xhr2.send();
Kristin Anthony
Kristin Anthony
11,099 Points

Don't know if you still need help but your problem is that you closed the list tag incorrectly. It's currently this:

<li/>

And should be this

</li>

1 Answer

Jordan Gauthier
Jordan Gauthier
5,552 Points

See Kristin Anthony's response.

Your closing li tag is not correct. Instead of <li/>, it should be </li>