Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7579917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:47:19+00:00 2026-05-30T17:47:19+00:00

Why xhr.responseXML.getElementsByClassName(‘clazz’) doesn’t work? Here is the js: var xhr = new XMLHttpRequest(); xhr.onreadystatechange

  • 0

Why xhr.responseXML.getElementsByClassName(‘clazz’) doesn’t work?

Here is the js:

    var xhr = new XMLHttpRequest();

    xhr.onreadystatechange = function() {
         if (xhr.readyState == 4) {
            var happiness = xhr.responseXML.getElementsByClassName('clazz');

            //Uncaught TypeError: Cannot read property 'innerHTML' of undefined
            console.log("happiness ? " + happiness[0].innerHTML);
         }
    };

    //xhr.overrideMimeType("application/xml");
    xhr.open("GET", "xhr.php", true);
    xhr.send(null);

xhr.php contains the following:

<?php

header('Content-type: application/xml; charset=UTF-8');
echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n";
?>

<!DOCTYPE html>
<html>
<head>
<title>I'm the XHR response</title>
</head>
<body>

<div class="clazz">xhr happiness</div>

<div id="x" class="y"></div>

</body>
</html>

can you please explain the following output in the Chrome console?

xhr.responseXML.getElementById('x')
<div id="x" class="y"></div>

xhr.responseXML.getElementsByClassName('y')
[]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T17:47:20+00:00Added an answer on May 30, 2026 at 5:47 pm

    xhr.responseXML is XML nodes and not HTML elements which means that class attribute has not a special meaning.

    However, when I added xmlns declaration to the html tag, the getElementsByClassName on responseXML just worked (tested on Chrome).

    <html xmlns="http://www.w3.org/1999/xhtml">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

function connectTo(url) { var xhr = new XMLHttpRequest(); xhr.open(GET, url, false); xhr.onreadystatechange = function
function fetchXmlDoc(uri) { var xhr = new XMLHttpRequest(); var async = false; xhr.open(GET, uri,
I have such part of code: var xhr = new XMLHttpRequest(); xhr.open(POST, http://someurl.com, true);
i have the following javascript in my webpage: var xhr = new XMLHttpRequest(); xhr.open('GET',
var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://static.reddit.com/reddit.com.header.png', true); xhr.responseType = 'arraybuffer'; xhr.onload = function(e)
I've got the following synchronous request... var xhr = new XMLHttpRequest(); xhr.open(GET,uri,false); xhr.send(null); I've
I defined a XMLHttpRequest in the Cloud9 IDE like this: var xhr = new
Have you ever used an XHR object to intercept onreadystatechange with a readyState different
I want to upload a file using an ajax request var xhr = new
for (var i = 0; i < 5; ++i) { var xhr; if (window.XMLHttpRequest)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.