PDA

View Full Version : AJAX and getElementById


otnj2ee
05-14-2009, 12:01 AM
I called the AJAX and it returns:

"<root><option id=\"test\" value=\"abc\">XYZ</option></root>"


In the front end::

if((req.readyState == 4 ) && req.status == 200) {

var answer =req.responseXML ;

alert(answer.getElementById('test').getAttribute("value") ); //It never displayed anything

//But

alert(answer.getElementsByTagName("option")[0].getAttribute("value") );

//worked and displayed the: abc
}

--Why does getElementById not work?


Thanks

Scott

bdl
05-14-2009, 01:47 PM
There is no "id" attribute defined in the XML DOM. It's not an HTML/XHTML document!

Google search : responseXML getElementById (http://www.google.com/search?&q=responseXML%20getElementById)
w3schools XML DOM (http://www.w3schools.com/dom/default.asp)

A1ien51
05-14-2009, 06:05 PM
If that is all that is returned, that is an invalid XML document.

Eric

otnj2ee
05-14-2009, 06:16 PM
Thanks for the response.

Another question regarding to the <!DOCTYPE...

When is the <!DOCTYPE required?

For the AJAX returned string which does not have the <!DOCTYPE, but following processing of it has no problem at all.


Thanks

Scott

otnj2ee
05-14-2009, 07:08 PM
To discuss with A1ien51, yes, the server returned:

"<root><option id=\"test\" value=\"abc\">XYZ</option></root>"

Now use AJAX, request.responseXML to follow-up, it actually works:

var ret = request.responseXML;

var value= ret.getElementsByTagName("option")[0].getAttribute("value");

alert(value); //This will display: "abc"


So it appears to me that the returned string is a valid XML string.


Thanks

Scott