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 trialOctavian Runceanu
6,143 PointsEmployee list is not displayed correctly
The employee list looks good, i mean it's send to html in a pretty good shape. But I believe it's a problem with the CSS because I do not have content before the person's name.
IN/OUT is not displayed
widget.js -
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (){
if (xhr.readyState === 4){
var employees = JSON.parse(xhr.responseText);
var statusHTML = '<ul class="bulleted">';
for (var i=0; i<employees.length; i++){
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();
https://ibb.co/K6JV2T2 - Here is the link with a printscreen with how it's displayed.
Can anyone notice any mistake?
Thanks in advance!
2 Answers
Octavian Runceanu
6,143 PointsApparently the problem was due to MAMP because it received an update for PHP. And the system did not behave as expected, so for this reason it was not displayed correctly. The code is correct.
Brendan O'Connor
4,091 PointsYeah, maybe i'm having the same problem...Octavian Runceanu or anyone else able to take a look? Thanks for considering (and all the notation is here to help me document for the future--may be a little overkill, but trying to make sure i remember it and understand it whenever i may circle back):
//Step 1: create XHR request
var xhr = new XMLHttpRequest();
//Step 2: create call back / event handler
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
//parse returned string of responseText to transform to JavaScript
const employees = JSON.parse(xhr.responseText);
//create statusHTML variable w/ul element
const statusHTML = '<ul class="bulleted">';
//for loop to go through JSON list of employees
for (var i=0; i<employees.length; i++) {
if (employees[i].inoffice === true) {
//add class (in or out) to statusHTML; as reminder, += means take current value & add something to it, so add list item w/this class if true
statusHTML += '<li class="in">';
} else {
statusHTML += '<li class="out">';
}
//add employees name to statusHTML
statusHTML += employees[i].name;
//add closing </li> tag to statusHTML
statusHTML += '</li>';
}
//add closing </ul> tag to statusHTML
statusHTML += '</ul>';
//selected div tag by "employeeList" ID, & insert innerHTML that = statusHTML
document.getElementById("employeeList").innerHTML = statusHTML;
}
};
//Step 3: open request
xhr.open("GET", "data/employees.json");
//Step 4: send request
xhr.send();
As one other note of context, this is the error message i'm receiving in the console:
Uncaught TypeError: invalid assignment to const 'statusHTML' onreadystatechange http://port-80-i7ghrtf9vg.treehouse-app.com/js/widget1.js:20 EventHandlerNonNull* http://port-80-i7ghrtf9vg.treehouse-app.com/js/widget1.js:5 widget1.js:20:11 onreadystatechange http://port-80-i7ghrtf9vg.treehouse-app.com/js/widget1.js:20 (Async: EventHandlerNonNull) <anonymous> http://port-80-i7ghrtf9vg.treehouse-app.com/js/widget1.js:5
Brendan O'Connor
4,091 PointsWell, ha...that didn't take long, funny how sometimes right after you go through the trouble of asking a Q it clarifies.
I changed my 2 const variables to var and it worked--why would that be though?