spacer

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

home / experts / javascript / column41


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

Developer News
Microsoft Shows Some Ankle With Visual Studio
Gentoo Linux Cancels Distribution
It's Official: Windows 7 at PDC, WinHEC

Navigating an Unordered List

The Document Object Model tree includes a node for every tag and for every textual entry. You can start "climbing" the tree only from those nodes that have been assigned the ID attribute. Once you are on the DOM tree, you can reach any node of the tree, no matter how far it is from the starting node. Examine again the object drawing of the <UL> 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 <UL> tag. You can reach this child using bodyNode.firstChild or bodyNode.childNodes[0]. The <UL> node has three children, one for each of the <LI> tag. You can go to the first <LI> tag using bodyNode.firstChild.firstChild or bodyNode.firstChild.childNodes[0]. You may reach the second child via bodyNode.firstChild.childNodes[1]. You may also access the third (and last) child by either bodyNode.firstChild.childNodes[2] or bodyNode.firstChild.lastChild.

We have labeled every one of the <LI> tags with a unique ID: bullet1Node, bullet2Node, and bullet3Node. Now, suppose you start navigating the tree from bullet1Node. You can reach the second bullet (an <LI> node) using the nextSibling property: bullet1Node.nextSibling. The third <LI> tag is reached via:

bullet1Node.nextSibling.nextSibling

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

bullet1Node.nextSibling.nextSibling.childNodes[0]

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

bullet3Node.previousSibling.previousSibling

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

bullet3Node.previousSibling.previousSibling.childNodes[0]

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

bodyNode.firstChild.childNodes[0].firstChild 

bodyNode.firstChild.childNodes[1].firstChild 

bodyNode.firstChild.childNodes[2].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 <LI> tag to the <BODY> root tag, you would use bullet1Node.parentNode.parentNode, bullet2Node.parentNode.parentNode, or bullet3Node.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

We have programmed some of the above queries into a JavaScript script in the HTML document we have presented earlier. It's not that trivial to demonstrate that a query is working. If you try to just print a query, all you get is that the result is an [object]. There are several ways to go around it. We chose to just print the nodeName of the object. The nodeName property displays the HTML tag type (examples: <LI>, <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: one <UL> 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


JupiterOnlineMedia

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

Solutions
Whitepapers and eBooks
IBM Whitepaper: Innovative Collaboration to Advance Your Business
Internet.com eBook: Real Life Rails
Avaya Article: Call Control XML - Powerful, Standards-Based Call Control
Internet.com eBook: The Pros and Cons of Outsourcing
Go Parallel Article: Scalable Parallelism with Intel(R) Threading Building Blocks
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
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
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
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Go Parallel Video: Intel(R) Threading Building Blocks: A New Method for Threading in C++
HP Video: Is Your Data Center Ready for a Real World Disaster?
Microsoft Partner Portal Video: Microsoft Gold Certified Partners Build Successful Practices
HP On Demand Webcast: Virtualization in Action
Go Parallel Video: Performance and Threading Tools for Game Developers
Rackspace Hosting Center: Customer Videos
Intel vPro Developer Virtual Bootcamp
HP Disaster-Proof Solutions eSeminar
HP On Demand Webcast: Discover the Benefits of Virtualization
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Microsoft Download: Silverlight 2 Software Development Kit Beta 2
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt
Iron Speed Designer Application Generator
Microsoft Download: Silverlight 2 Beta 2 Runtime
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
IBM IT Innovation Article: Green Servers Provide a Competitive Advantage
Microsoft Article: Expression Web 2 for PHP Developers--Simplify Your PHP Applications
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Controllers: Programming Application Logic - Part 2 · How to Use JavaScript to Validate Form Data · Controllers: Programming Application Logic
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Sprint Launches Mobile WiMAX Network · Albatron Downsizes with the KI780G Mini-ITX Motherboard · Can't Find a Wi-Fi Network? Make Your Own.


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

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