How to get attribute value from XML with Java Script?

Viewed 58

I'm using an application which has XML and Java Script plugin inside. I need to get value of specified attribute. I'm triying to get TEST element's id value with the following command but wrong value is coming.

System.log(document.getDocumentElement("TEST").getAttributes().getNamedItem('id').value);

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Task id="123456">   
    <TEST href="https://portal/api/product/5fb26d98-3590-4b7a-9af6-264468b33a83" id="5fb26d98-3590-4b7a-9af6-264468b33a83" name="TEST" type="application/xml"/>
    <TEST2 href="https://portal/api/product/1fb26d98-3590-4b7a-9af6-264468b33a80" id="1fb26d98-3590-4b7a-9af6-264468b33a80" name="TEST2" type="application/xml"/>
</Task>

When I try to this command, "123456" value is coming. What is wrong ?

2 Answers

getDocumentElement doesn't accept any arguments (it isn't querySelector or getElementsbyTagName) so the string "TEST" is ignored.

It gets the document element, also known as the root element, which is Task in this case.

I think Below statement can give you a desire output:

var x = document.getElementsByTagName("TEST")[0].getAttribute("id"); 
console.log(x);
Related