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 Programming AJAX Processing JSON Data

Dou Sun
Dou Sun
2,754 Points

Why can't I use DOM to add element and class in this example?

As mentioned in the title, why can't I use DOM to add the 'ul' and 'li' elements to the page? Here is my code:

let xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState === 4){ let employees = JSON.parse(xhr.responseText); let statusHTML = document.createElement('ul'); statusHTML.className = 'bulleted'; let statusDiv = document.getElementById('employeeList'); statusDiv.appendChild(statusHTML);

for(let i = 0; i < employees.length; i++){
  let employee = document.createElement('li');
  employee.textContent = employees[i].name;
  if (employees[i].inoffice = false){
    employee.className = 'out';
  } else{
  employee.className = 'in';
} 
  statusHTML.appendChild(employee);

} }; xhr.open('GET', 'data/employees.json'); xhr.send();

Steven Parker
Steven Parker
231,269 Points

To facilitate easy replication of your issue, make a snapshot of your workspace and post the link to it here.

1 Answer

Steven Parker
Steven Parker
231,269 Points

At first glance, I notice that the conditional expression contains an assignment ("=") instead of a comparison ("==").

But if that's not be the whole issue, please post a snapshot and I'll take another look.