Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Sharina V. Jones
11,459 PointsAJAX Basics: Code Challenge Stage 2
The challenge is
Now that the server has responded with the data, you need to add > it to the page. First select the div with the ID of 'sidebar'. You can >select page elements with IDs like this: >document.getElementById('idName');
This is the code I entered:
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
}
};
xhr.open('GET', 'sidebar.html');
xhr.send();
document.getElementById('sidebar');
I keep getting Bummer! To select the <div> tag with the 'sidebar' ID, you need to pass the ID name as a string to the getElementById method, like this: document.getElementById('sidebar').
Please help!
4 Answers

lordcozycat
11,940 PointsYour document.getElementById('sidebar')
is conditional on the "if" statement. Just move that line inside if (xhr.readyState === 4 && xhr.status === 200) { put it here }

Michelle Pepe
Courses Plus Student 6,124 Pointsvar xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === 4 && xhr.status === 200) {
document.getElementById('sidebar');
}
};
xhr.open('GET', 'sidebar.html');
xhr.send();

Dave McFarland
Treehouse TeacherTo put code into a forum post use triple back ticks -- ``` — around the code. I fixed your code here, but in the future here's a forum discussion that describes how to add HTML, CSS, JavaScript or other code to the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum

Michelle Pepe
Courses Plus Student 6,124 PointsThank you Dave.

Ary de Oliveira
28,295 PointsDry Answer:
Challenge Task 1 of 2
In app.js, we've added an AJAX callback function. You need to complete the request. Add the code to open the AJAX request using the GET method and pointing to the 'footer.html' file:
var request = new XMLHttpRequest(); request.onreadystatechange = function () { if (request.readyState === 4) { document.getElementById("footer").innerHTML = request.responseText; } }; request.open('GET', 'footer.html');

Sharina V. Jones
11,459 PointsThanks!
Dave McFarland
Treehouse TeacherDave McFarland
Treehouse TeacherThe code has to go inside the conditional statement (and inside the callback function) just like Bryan H points out. This is necessary because AJAX requests are "asynchronous" meaning that the rest of the program and your web page keeps doing its business between when the request is sent and the response returns. Putting
document.getElementById('sidebar');
last in the code above, doesn't make it run last. It actually runs BEFORE the server has time to respond to the AJAX request. That's the point of a callback (the function assigend to theonreadystatechange
event) to wait until the response comes in -- then and only then should you select the page element.