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) AJAX Concepts A Simple AJAX Example

Hello World
Hello World
12,904 Points

select the button using getElementsByTagName and it doesn't work?

My code is as below, i simply changed "document.getElementById("load")" to "getElementsByTagName("button")", it doesn't work. it shows "cannot set display of undefined" in the console.

 var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
      if(xhr.readyState === 4) {
        document.getElementById("ajax").innerHTML = xhr.responseText;
      }
    };
    xhr.open("GET", "sidebar.html");
    function sendAjax() {
      xhr.send();
      document.getElementsByTagName("button").style.display = "none";
    }

1 Answer

Jake Adams
Jake Adams
1,608 Points

My JavaScript and DOM manipulation skills are rusty, but I believe you have to loop over the results of the getElementsByTagName before calling .style. That method returns an array of elements and an array has no style property.

You can optionally just access the first element in that array like

document.getElementsByTagName("button")[0].style.display = "none";

That's making an assumption that the first button on the page is the one you want to hide.

Hello World
Hello World
12,904 Points

yes, your are right, thank you.