2

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 2

1

I think Below statement can give you a desire output:

var x = document.getElementsByTagName("TEST")[0].getAttribute("id"); 
console.log(x);
Sign up to request clarification or add additional context in comments.

2 Comments

I am getting this error "TypeError: Cannot call method "getAttribute" of null".
it works with this ; document.getElementsByTagName("TEST").item(0).getAttribute("id"); Thank you very much !
1

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.

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.