Results 1 to 4 of 4

Thread: [Javascript] Getting contents of an element in a node list

  1. #1

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    [Javascript] Getting contents of an element in a node list

    I've read in an XML document and have got a node list using getelementsbytagname.

    This works ok as the length of the nodelist is 18, which is right having checked the xml document i'm reading in.

    Trouble is, i can't workout how to access the contents of the element, i.e.

    <title mode="escaped" type="text/html">Long time no see</title>

    I've tried the following, but nothing works. (i'm testing using the alert statement).

    MyNodeList.item(0).value
    MyNodeList.item(0).innerHTML

    I've also tried accessing other elements besides 0.

    When i do alert(MyNodeList.item(0)) it correctly displays a message box containing [Object Element], so i know the element's there!

    Any help would be greatly appreciated!
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


  2. #2
    Lively Member {yak}'s Avatar
    Join Date
    Aug 2005
    Posts
    119

    Re: [Javascript] Getting contents of an element in a node list

    What happends when you do:

    alert(MyNodeList.item(0).childNodes[1].firstChild.nodeValue);
    {yak}

  3. #3
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [Javascript] Getting contents of an element in a node list

    That is because the node contains another node. A text node object which is dreived from a node object. To access it you need to to do the following:
    Code:
    MyNodeList.item(0).childNodes[0].nodeValue
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  4. #4

    Thread Starter
    Not NoteMe SLH's Avatar
    Join Date
    Mar 2002
    Location
    192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
    Posts
    3,051

    Re: [Javascript] Getting contents of an element in a node list

    Cool that works, thanks Adam.

    Just a quick question though, am i right in thinking that you can only get the innerHTML of a leaf element then?? If so, what if i want just a branch of the document tree???
    Quotes:
    "I am getting better then you guys.." NoteMe, on his leet english skills.
    "And I am going to meat her again later on tonight." NoteMe
    "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
    "my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
    Have I helped you? Please Rate my posts.


Posting Permissions

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



Click Here to Expand Forum to Full Width