DHTML
  Home arrow DHTML arrow Page 2 - Rough Guide To The DOM (part 1)
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
OLM
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DHTML

Rough Guide To The DOM (part 1)
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2001-04-09

    Table of Contents:
  • Rough Guide To The DOM (part 1)
  • Back To Basics
  • Navigating The Family Tree
  • What's In A Name?
  • Ducks In A Row
  • Changing Things Around
  • Alternatives
  • Shazam!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    Rough Guide To The DOM (part 1) - Back To Basics
    (Page 2 of 8 )

    We'll start with the basics - a very simple HTML page.

    <html><head></head><body bgcolor="white"><div id="a" style="font-family: Arial; color: white; background: black">Wassup?</div></body></html>

    Let's alter the font colour of the text within the <div>. In InternetExplorer, this would typically be accomplished with

    <script language="JavaScript"> document.all.a.style.color = "red"; </script>

    Here's the code I would use in Mozilla:

    <script language="JavaScript"> var obj = document.childNodes[0].childNodes[1].childNodes[0]; obj.style.color = "red"; </script>

    An explanation is in order here. Under the new DOM, every element in anHTML document is part of a "tree", and you can access each and everyelement by navigating through the tree "branches" until you reach thecorresponding "node". Given that, here's my representation of the HTMLdocument above, in "tree" form.

    document | -- <html> | -- <head> | -- <body> | -- <div>

    Now, to get to the <div>, I need to:

    1. start at the top ("document");

    2. move down to the main branch - the <html> tag, or"document.childNodes[0]";

    3. then to the second sub-branch - the <body> tag or"document.childNodes[0].childNodes[1]";

    4. then to the <div> - "document.childNodes[0].childNodes[1].childNodes[0]";

    At this point, I have successfully navigated my way to the <div> element inthe document tree. A quick way of verifying this is to use an alert() onthe object

    <script language="JavaScript"> var obj = document.childNodes[0].childNodes[1].childNodes[0]; alert(obj.nodeName); obj.style.color = "red"; </script>

    which displays the name of the tag - DIV - in an alert box.

    At this point, I can begin futzing with object attributes - in the exampleabove, I've altered the "color" style attribute. Don't worry about this forthe moment; simply verify that you have understood the manner in which Inavigated through the document tree to reach the DIV.

    More DHTML Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    DHTML ARTICLES

    - Rough Guide To The DOM (part 2)
    - Rough Guide To The DOM (part 1)
    - Filters And Transitions In IE5
    - Understanding Embedded Fonts

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway