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 Check for the correct ready state

Sharina V. Jones
Sharina V. Jones
11,459 Points

AJAX 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

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

Dave McFarland
Dave McFarland
Treehouse Teacher

The 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 the onreadystatechange event) to wait until the response comes in -- then and only then should you select the page element.

var 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
Dave McFarland
Treehouse Teacher

Michelle Pepe

To 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

Thank you Dave.

Dry 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');