Results 1 to 5 of 5

Thread: How to access the childNodes value

  1. #1
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to access the childNodes value

    Hi All,
    Need to access the value of a childNodes in the following code:

    Code:
    <HTML>
    <HEAD>
    <TITLE>My Document</TITLE>
    </HEAD>
    <BODY BGCOLOR="lightgreen">
    <DIV ID="myDiv">Testing</DIV>
    <script>
    var values=document.childNodes[1].childNodes[0].innerText;
    document.write(values);
    </script>
    </BODY>
    </HTML>
    I need the way to get the value of "myDiv".
    When I loaded the code in Chrome browser, I got the following error: "Uncaught TypeError: Cannot read property 'childNodes' of undefined"
    Please, what should the correct syntax?
    Thanks in advance.

    Regards,
    d4d4n9
    Last edited by Beverleyh; 07-26-2013 at 04:55 PM. Reason: Formatting

  2. #2
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,982
    Thanks
    16
    Thanked 311 Times in 309 Posts
    Blog Entries
    11

    Default

    I'm not a JavaScript expert but if you're trying to access the content inside the div, would getElementById be better?
    Code:
     var values = document.getElementById("myDiv").innerHTML;
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. The Following User Says Thank You to Beverleyh For This Useful Post:

    d4d4n9 (07-26-2013)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    document.childNodes[1] does not exist (and so, is "undefined"). document has only one child: <html>

    So, <html> is node #0 of the document; and <body> is actually node #2 of <html> (#0 is the <head>, #1 is the carriage return (\r\n) between the <head> and <body>).

    As you may have realized at this point, Beverley is right - getElementById would be a much easier and more robust way to approach this task.
    Last edited by traq; 07-26-2013 at 08:37 PM.

  5. The Following User Says Thank You to traq For This Useful Post:

    d4d4n9 (07-26-2013)

  6. #4
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    @Beverleyh and traq
    Thanks for your responses. Right, it's easier to use Beverleyh's way.
    It's really nice.

  7. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    29,025
    Thanks
    44
    Thanked 3,210 Times in 3,172 Posts
    Blog Entries
    12

    Default

    You can also use:

    Code:
    document.documentElement
    for <html>

    and:

    Code:
    document.body
    for <body>

    And, if you're not interested in text nodes:

    Code:
    document.getElementsByTagName('*')
    Will get you a node list of all of the tags in the document. Similarly you can go after specific tags:

    Code:
    document.getElementsByTagName('head')[0]
    will get you the head, Or:

    Code:
    document.getElementsByTagName('div')
    Will get you all of the divs, which you could then iterate over to find the one you want. Or if you already know, refer to it by its index, ex:

    Code:
    document.getElementsByTagName('div')[5]
    Which would be the 6th - if it exists.

    It really all depends upon what you're after, and how best to get a reference to it. Sometimes there are multiple ways. Well almost always, but sometimes two or more of these methods will be just as good as the other.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Replies: 3
    Last Post: 09-25-2010, 10:29 AM
  2. Replies: 1
    Last Post: 07-12-2010, 11:35 PM
  3. parentNode.childNodes not working in IE
    By JShor in forum JavaScript
    Replies: 4
    Last Post: 10-08-2009, 12:29 AM
  4. Replies: 1
    Last Post: 02-20-2009, 01:33 PM
  5. Replies: 3
    Last Post: 11-25-2005, 12:57 AM

Bookmarks

Posting Permissions

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