Problem Getting Elements By Tag Name

B

Bryan

Hello all,

I'm using XMLHttpRequest to get data from a PHP script. Everything
seems to be working... if I look at the result of "new
XMLSerializer().serializeToString(response)" with response being
"response = request.responseXML" I get the correct xml data. However,
when I try to do something like "var result =
response.getElementByTagName("result")" it doesn't work.

Any suggestions as to what's going on?
 
M

Martin Honnen

Bryan wrote:

I'm using XMLHttpRequest to get data from a PHP script. Everything
seems to be working... if I look at the result of "new
XMLSerializer().serializeToString(response)" with response being
"response = request.responseXML" I get the correct xml data. However,
when I try to do something like "var result =
response.getElementByTagName("result")" it doesn't work.

What happens exactly? getElementsByTagName returns a node list so you
should check result.length. If it is 0 then no elements were found.
We would need to see the XML to tell more.
 
B

Bryan

The returned XML looks like this:

<?xml version="1.0"?>
<results><cities><city id="1" name="Albuquerque" state="NM" desc="Duke
City" lat="0" lng="0"/></cities><result status="success"/></results>

I get this when I do the following:

var response = request.responseXML;
alert(new XMLSerializer().serializeToString(response));

I then do the following:

var result = response.getElementByTagName("result");
alert(result.length);

However, for some reason the alert never even pops up... like the code
never gets past assigning data to the result variable.

Thanks for your assistance.
 
M

Martin Honnen

Bryan wrote:

var result = response.getElementByTagName("result");
alert(result.length);

However, for some reason the alert never even pops up... like the code
never gets past assigning data to the result variable.

Check the JavaScript console (or error console) then, tell us which
error messages it shows, which browser exactly does that, whether other
browsers give the same errors.
 
B

Bryan

The JavaScript Console gives me the following error...

Error: response.getElementByTagName is not a function
 
B

Bryan

Sorry, forgot to say that this is happening in Firefox 1.5.0.7

I'm developing on a linux laptop, so I cannot test using another
browser, such as IE.
 
B

Bryan

If I use

var result = response.getElementsByTagName("result")[0]

rather than

var result = response.getElementByTagName("result") <-- no "s" on
Element

everything works fine. However, I thought there was a function called
getElementByTagName that just grabbed the first occurance... not so?
 
M

Martin Honnen

Bryan said:
However, I thought there was a function called
getElementByTagName that just grabbed the first occurance... not so?

No. MSXML 3 as used by IE 6 has a method selectSingleNode which takes an
XPath expression and returns the first matching node. But the W3C Core
DOM only has getElementsByTagName.
 
M

Matthom

getElementsByTagName returns an array. So just reference the first
occurance of the "result" element:

getElementsByTagName[0]
 
M

Matthom

Sorry, that would be:

getElementsByTagName("result")[0]


getElementsByTagName returns an array. So just reference the first
occurance of the "result" element:

getElementsByTagName[0]




Martin said:
No. MSXML 3 as used by IE 6 has a method selectSingleNode which takes an
XPath expression and returns the first matching node. But the W3C Core
DOM only has getElementsByTagName.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top