Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts

    AJAX and getElementById

    I called the AJAX and it returns:

    "<root><option id=\"test\" value=\"abc\">XYZ</option></root>"


    In the front end::

    if((req.readyState == 4 ) && req.status == 200) {

    var answer =req.responseXML ;

    alert(answer.getElementById('test').getAttribute("value") ); //It never displayed anything

    //But

    alert(answer.getElementsByTagName("option")[0].getAttribute("value") );

    //worked and displayed the: abc
    }

    --Why does getElementById not work?


    Thanks

    Scott
    Last edited by otnj2ee; 05-14-2009 at 12:03 AM.

  • #2
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    There is no "id" attribute defined in the XML DOM. It's not an HTML/XHTML document!

    Google search : responseXML getElementById
    w3schools XML DOM

  • #3
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    If that is all that is returned, that is an invalid XML document.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #4
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks for the response.

    Another question regarding to the <!DOCTYPE...

    When is the <!DOCTYPE required?

    For the AJAX returned string which does not have the <!DOCTYPE, but following processing of it has no problem at all.


    Thanks

    Scott

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    179
    Thanks
    17
    Thanked 0 Times in 0 Posts
    To discuss with A1ien51, yes, the server returned:

    "<root><option id=\"test\" value=\"abc\">XYZ</option></root>"

    Now use AJAX, request.responseXML to follow-up, it actually works:

    var ret = request.responseXML;

    var value= ret.getElementsByTagName("option")[0].getAttribute("value");

    alert(value); //This will display: "abc"


    So it appears to me that the returned string is a valid XML string.


    Thanks

    Scott


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •