spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column40


The Document Object Model (DOM), Part I (8)

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

Navigating a Table

The table's Document Object Model supports a good structure for utilizing the DOM's properties. As shown earlier in this column, the properties childNodes, firstChild, lastChild, nextSibling, and previousSibling can take you from any node of the tree to any other node. Following the tree diagram will help you better understand the navigation steps we take on the tree.

We start from the root, <TABLE>. To go down to the second row of the table, you would go like this:

tableNode.firstChild.childNodes[1]

To reach the first cell of the row, you would go:

tableNode.firstChild.childNodes[1].childNodes[0]

To reach the content of the second cell in the first row:

tableNode.firstChild.firstChild.childNodes[1].firstChild

There are several ways to go back from the the third row to the root:

tr3Node.parentNode.parentNode
tr3Node.previousSibling.parentNode.parentNode
tr3Node.previousSibling.previousSibling.parentNode.parentNode

One-way tickets from the root to the content of each of the six cells are as follows:

tableNode.firstChild.firstChild.firstChild.firstChild
tableNode.firstChild.firstChild.childNodes[1].firstChild
tableNode.firstChild.childNodes[1].firstChild.firstChild
tableNode.firstChild.childNodes[1].childNodes[1].firstChild
tableNode.firstChild.childNodes[2].firstChild.firstChild
tableNode.firstChild.childNodes[2].childNodes[1].firstChild

Round trips to the six cells will look like this:


tableNode.firstChild.firstChild.firstChild.firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
tableNode.firstChild.firstChild.childNodes[1].firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
tableNode.firstChild.childNodes[1].firstChild.firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
tableNode.firstChild.childNodes[1].childNodes[1].firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
tableNode.firstChild.childNodes[2].firstChild.firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)
tableNode.firstChild.childNodes[2].childNodes[1].firstChild.parentNode.
  parentNode.parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

Make sure you understand every turn along these routes. If you don't agree with one of them, you can see them in real action. We print the nodeName property of the printed object as a proof to the correctness of these queries.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business


Created: May 31, 1999
Revised: May 31, 1999

URL: http://www.webreference.com/js/column40/navigatetable.html