0

The following code brings back my id.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
  }
};
xhttp.open("GET", "https://brewslocal.com/brewery-images-xml", true);
xhttp.send();

function myFunction(xml) {
  var xmlDoc = xml.responseXML;
  document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].id;
}
<div id="photoBox"></div>

When I change to the next attribute imageurl it comes back undefined.

var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
  if (this.readyState == 4 && this.status == 200) {
    myFunction(this);
  }
};
xhttp.open("GET", "https://brewslocal.com/brewery-images-xml", true);
xhttp.send();

function myFunction(xml) {
  var xmlDoc = xml.responseXML;
  document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].imageurl;
}
<div id="photoBox"></div>

Any ideas?

0

2 Answers 2

4

You can use Element.getAttribute('attributeName') to get the attribute value from this custom HTML element.

function myFunction(xml) {
  var xmlDoc = xml.responseXML;
  document.getElementById("photoBox").innerHTML = xmlDoc.getElementsByTagName("image")[0].getAttribute('imageurl');
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can do like this

function myFunction(xml) {
var xmlDoc = xml.responseXML;
var x = xmlDoc.getElementsByTagName('title')[0];
var y = x.childNodes[0];
document.getElementById("demo").innerHTML =
y.nodeValue; 

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.