SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Question about DOM object's childNodes

    Hi,

    I was using javascript on a page to browse through it's elements using childNodes properties, and I have some question on this.

    I'm browsing through a nested table element and my goal is to get to a specific column of a table. I don't have any control over the page, so I can't add an ID and simply use getElementById. I wouldn't even use table for layout, if I could control it.

    Anyways, when I'm using the "childNodes[objRef]" syntax, it's not working. After doing some investigation, found out that the childNodes only have a method named "item()".

    Now, my question is what's the difference between using the array (childNodes[aNumber]) and the item() method (childNodes.item()). Why don't I have the array option available on certain elements, while others do.

    And lastly, how do you use the childNodes.item() option?

    I hope, I was able to explain it clearly. If not, let me know and I'll see if I can gather some screenshots for you.

    Thanks for your help.
    With Regards
    Pman
    http://www.pmansLab.com

  2. #2
    We're from teh basements.
    Join Date
    Apr 2007
    Posts
    1,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    item() is the correct way to access items in a NodeList or similar collection. A collection is a special class of DOM object for containing other DOM objects, not a simple JavaScript array. As such, you need to use the collection's own methods to operate on it. Although the JavaScript array syntax will work, it's not clear why it's working in some cases but not others.

  3. #3
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,716
    Mentioned
    103 Post(s)
    Tagged
    4 Thread(s)
    Here is the common understanding about child nodes.

    When the childnodes are as an html nodelist the array notation works as expected. The item method also works too.

    When the childnodes are as an xml nodelist, the item method must be used.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #4
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    68
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks so much for your reply. Now I understand it better.
    With Regards
    Pman
    http://www.pmansLab.com


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
  •