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 trialGinger Williams
6,409 PointsHow do you log each iteration of workers in the id?
I am trying to simply list the employees and their titles in an employee-list container. It iterates through but only writes the last one to the dom.
</code> var x = new XMLHttpRequest(); x.onreadystatechange = function(){ if(x.readyState === 4){ var employees = JSON.parse(x.responseText);
for(var i=0; i<employees.length; i++ ){
var name = employees[i].name;
var title = employees[i].title;
//does not list each iteration
document.getElementById('employee-list').innerHTML = name + title;
//lists each iteration
console.log(name+title);
}
}
} x.open('GET', 'data/employees.json'); x.send(); </code>
1 Answer
jason phillips
18,131 Points document.getElementById('employee-list').innerHTML = name + title;
this line overwrites the content every time, so only the last iteration of the loop will show. Try something like this:
document.getElementById('employee-list').innerHTML += name + title;
Ginger Williams
6,409 PointsGinger Williams
6,409 PointsI didn't know that was allowed =D Thanks!