By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,930 Members | 1,323 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,930 IT Pros & Developers. It's quick & easy.

Problem Getting Elements By Tag Name

P: n/a
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
Share this Question
Share on Google+
10 Replies


P: n/a


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

P: n/a
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

P: n/a


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

P: n/a
The JavaScript Console gives me the following error...

Error: response.getElementByTagName is not a function

Sep 24 '06 #5

P: n/a
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

P: n/a


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

P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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.