469,282 Members | 2,372 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Problem Getting Elements By Tag Name

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?

Sep 23 '06 #1
10 12487


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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 23 '06 #2
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.

Sep 24 '06 #3


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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 24 '06 #4
The JavaScript Console gives me the following error...

Error: response.getElementByTagName is not a function

Sep 24 '06 #5
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.

Sep 24 '06 #6


Bryan wrote:
The JavaScript Console gives me the following error...

Error: response.getElementByTagName is not a function
Right that method in the DOM is called getElementsByTagName and not
getElementByTagName. So
response.getElementsByTagName('element-name')
will do.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 24 '06 #7
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?

Sep 24 '06 #8

Bryan wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 24 '06 #9
getElementsByTagName returns an array. So just reference the first
occurance of the "result" element:

getElementsByTagName[0]


Martin Honnen wrote:
Bryan wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 25 '06 #10
Sorry, that would be:

getElementsByTagName("result")[0]

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

getElementsByTagName[0]


Martin Honnen wrote:
Bryan wrote:
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.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Sep 25 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Sowen | last post: by
11 posts views Thread by ned786 | last post: by
11 posts views Thread by Marcus Jacobs | last post: by
1 post views Thread by infiniti | last post: by
7 posts views Thread by Trickynick1001 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.