spacer

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

home / experts / javascript / column41


The Document Object Model (DOM), Part II (5)

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

Navigating a Descriptive List

The Object Model of a descriptive list (<DL>) is much different than an unordered list (<UL>). Examine again the object drawing of the <DL> list presented earlier. The arrows shown are the navigational routes you can take to reach different nodes of the tree. We have assigned bodyNode to the ID attribute of the <BODY> tag. From the root <BODY> you can go to its only child, the <DL> tag. You can reach this child using bodyNode.firstChild or bodyNode.childNodes[0]. The <DL> node has six children, three for the <DT> tags and three for the <DD> tags. You can go to the first <DT> tag using bodyNode.firstChild.firstChild or bodyNode.firstChild.childNodes[0]. Since the <DT> tags interleave the <DD> tags, you may reach the second <DT> tag via bodyNode.firstChild.childNodes[2]. You may also access the third (and last) <DT> tag by bodyNode.firstChild.childNodes[4]. The three <DD> tags may be reached via the following expressions:

bodyNode.firstChild.childNodes[1]

bodyNode.firstChild.childNodes[3]

bodyNode.firstChild.childNodes[5]

We have labeled every one of the <DT> tags with a unique ID: header1Node, header2Node, and header3Node. Similarly, we have labeled every one of the <DD> tags with a unique ID as well: body1Node, body2Node, and body3Node. Now, suppose you start navigating the tree from header1Node. You can reach the second bullet (an <DT> node) using the nextSibling property: header1Node.nextSibling.nextSibling. The third <DT> tag is reached via:

header1Node.nextSibling.nextSibling.nextSibling.nextSibling

Suppose we want to reach the text node of the third bullet's body. Remembering that each bullet has a single text node child, we can accomplish it by:


header1Node.nextSibling.nextSibling.nextSibling.nextSibling.
  nextSibling.childNodes[0]
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

Suppose now that we start our navigation with the third <DD> tag. We can go back to the first <DD> element by using the previousSibling property:

body3Node.previousSibling.previousSibling.previousSibling.previousSibling

We can access the textual content of the first child by going:


body3Node.previousSibling.previousSibling.previousSibling.
  previousSibling.previousSibling.childNodes[0]
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

Let's start again at the <BODY> tag. It has six grand grandchildren. You can reach them via:

bodyNode.firstChild.childNodes[0].firstChild 

bodyNode.firstChild.childNodes[1].firstChild 

bodyNode.firstChild.childNodes[2].firstChild 

bodyNode.firstChild.childNodes[3].firstChild 

bodyNode.firstChild.childNodes[4].firstChild 

bodyNode.firstChild.childNodes[5].firstChild 

Another navigation direction is the child to parent direction. You can reach each node's parent via the parentNode property. To go from each of the <DT> tag to the <BODY> root tag, you would use header1Node.parentNode.parentNode, header2Node.parentNode.parentNode, or header3Node.parentNode.parentNode. Similarly, to go from each of the <DD> tag to the <BODY> root tag, you would use body1Node.parentNode.parentNode, body2Node.parentNode.parentNode, or body3Node.parentNode.parentNode. You may also take a round trip from the root <BODY> to its grand grandchild and back by using:


bodyNode.firstChild.firstChild.firstChild.parentNode.
  parentNode.parentNode
  // (The above two lines should be joined as one line.
  // They have been split for formatting purposes.)

We have programmed some of the above queries into a JavaScript script in the simple document we have presented earlier. As we explained earlier, we chose to just print the nodeName of the object. The nodeName property displays the HTML tag type (examples: <DD>, <BODY>, <FONT>) for tag nodes, and the string #text for text nodes. Notice that this script actually modify the page and hence its Document Object Model. In effect, there are two top-level children beneath the <BODY> tag: a <DL> tag and a <SCRIPT> tag. Not to complicate things, we have avoided using the lastChild property, and hence you won't notice the new <SCRIPT> child.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

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

Whitepapers and eBooks

Intel Whitepaper: Comparing Two- and Four-Socket Platforms for Server Virtualization
IBM Solutions Brief: Go Green With IBM System xTM And Intel
HP eBook: Simplifying SQL Server Management
IBM Contest: Are You the Next Superstar? Join the "Search for the XML Superstar" Contest to Find Out
Microsoft PDF: Top 10 Reasons to Move to Server Virtualization with Hyper-V
Microsoft PDF: Six Reasons Why Microsoft's Hyper-V Will Overtake Vmware
Microsoft Step-by-Step Guide: Hyper-V and Failover Clustering
Intel PDF: Quad-Core Impacts More Than the Data Center
Intel PDF: Virtualization Delivers Data Center Efficiency
Go Parallel Article: PDC 2008 in Review
Microsoft PDF: Top 11 Reasons to Upgrade to Windows Server 2008
Avaya Article: Communication-Enabled Mashups: Empowering Both Business Owners and IT
Intel Whitepaper: Building a Real-World Model to Assess Virtualization Platforms
  PDF: Intel Centrino Duo Processor Technology with Intel Core2 Duo Processor
Microsoft Article: Build and Run Virtual Machines with Hyper-V Server 2008
Go Parallel Article: Q&A with a TBB Junkie
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
IBM eBook: The Pros and Cons of Outsourcing
Internet.com eBook: Best Practices for Developing a Web Site
IBM CXO Whitepaper: The 2008 Global CEO Study "The Enterprise of the Future"
Avaya Article: Call Control XML in Action - A CCXML Auto Attendant
IBM CXO Whitepaper: Unlocking the DNA of the Adaptable Workforce--The Global Human Capital Study 2008
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
HP eBook: Guide to Storage Networking
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
webref The latest from WebReference.com Browse >
Popular JavaScript Framework Libraries: An Overview - Part 3 · Accessing Your MySQL Database from the Web with PHP · Working with the DOM Stylesheets Collection
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Fixing MySQL Replication · Firewall Guide: First Steps to Securing the Enterprise · VoxOx Tames the Tumultuous Communications Tangle


Created: June 14, 1999
Revised: June 14, 1999

URL: http://www.webreference.com/js/column41/navigatedllist.html