XML DOM getElementsByTagName() Method
Element Object
Example
The following code fragment loads "books.xml" into xmlDoc and gets the value of all <title> elements:
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
��� if (xhttp.readyState == 4 && xhttp.status == 200) {
������� myFunction(xhttp);
��� }
};
xhttp.open("GET", "books.xml", true);
xhttp.send();
function myFunction(xml) {
var x, i, attnode,
xmlDoc, txt;
xmlDoc = xml.responseXML;
txt = "";
x = xmlDoc.getElementsByTagName('title');
for (i = 0; i < x.length; i++) {
txt += x[i].childNodes[0].nodeValue + "<br>";
}
document.getElementById("demo").innerHTML = txt;
}
Output:
Everyday Italian
Harry Potter
XQuery Kick Start
Learning XML
Try it yourself »
Definition and Usage
The getElementsByTagName() method returns a NodeList of all a elements with a specified name.
Syntax
getElementsByTagName(name)
Parameter | Description |
---|---|
name | A string that specifies the tagname to search for. The value "*" matches all tags |
Element Object