Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Problem Getting Elements By Tag Name

Reply
Thread Tools

Problem Getting Elements By Tag Name

 
 
Bryan
Guest
Posts: n/a
 
      09-23-2006
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?

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      09-23-2006


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/
 
Reply With Quote
 
 
 
 
Bryan
Guest
Posts: n/a
 
      09-24-2006
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.

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      09-24-2006


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/
 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      09-24-2006
The JavaScript Console gives me the following error...

Error: response.getElementByTagName is not a function

 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      09-24-2006
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.

 
Reply With Quote
 
Bryan
Guest
Posts: n/a
 
      09-24-2006
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?

 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      09-24-2006

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/
 
Reply With Quote
 
Matthom
Guest
Posts: n/a
 
      09-25-2006
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/


 
Reply With Quote
 
Matthom
Guest
Posts: n/a
 
      09-25-2006
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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
getting all the input elements of a form tag yawnmoth XML 1 07-16-2008 11:34 PM
Using get elements by tag name in Sarissa scot.haynes@ukgateway.net Javascript 0 03-30-2006 09:52 PM
how do u invoke Tag b's Tag Handler from within Tag a's tag Handler? shruds Java 1 01-27-2006 03:00 AM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
getting name and value of tag in an XML. titusjava@yahoo.com XML 3 07-04-2005 04:53 PM



Advertisments