Problem Getting Elements By Tag Name

Discussion in 'Javascript' started by Bryan, Sep 23, 2006.

  1. Bryan

    Bryan Guest

    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?
     
    Bryan, Sep 23, 2006
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Sep 23, 2006
    #2
    1. Advertising

  3. Bryan

    Bryan Guest

    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.
     
    Bryan, Sep 24, 2006
    #3
  4. 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/
     
    Martin Honnen, Sep 24, 2006
    #4
  5. Bryan

    Bryan Guest

    The JavaScript Console gives me the following error...

    Error: response.getElementByTagName is not a function
     
    Bryan, Sep 24, 2006
    #5
  6. Bryan

    Bryan Guest

    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.
     
    Bryan, Sep 24, 2006
    #6
  7. Bryan

    Bryan Guest

    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?
     
    Bryan, Sep 24, 2006
    #7
  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/
     
    Martin Honnen, Sep 24, 2006
    #8
  9. Bryan

    Matthom Guest

    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/
     
    Matthom, Sep 25, 2006
    #9
  10. Bryan

    Matthom Guest

    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/
     
    Matthom, Sep 25, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    890
    Roedy Green
    Jan 2, 2006
  2. shruds
    Replies:
    1
    Views:
    932
    John C. Bollinger
    Jan 27, 2006
  3. Replies:
    3
    Views:
    434
    Martin Honnen
    Jul 4, 2005
  4. yawnmoth
    Replies:
    1
    Views:
    426
    Johannes Koch
    Jul 17, 2008
  5. Replies:
    0
    Views:
    106
Loading...

Share This Page