DHTML
  Home arrow DHTML arrow Page 6 - 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Rough Guide To The DOM (part 1) - Changing Things Around
    (Page 6 of 8 )

    Now that you know how to find your way to specific HTML elements in thedocument, it's time to learn how to manipulate them. Since most of thismanipulation involves altering tag attributes on the fly, the DOM offersthe getAttribute() and setAttribute() methods, which are designed expresslyfor this purpose.

    Consider the following modification to the example you just saw, which usesthese two methods to alter the font size and the text string.

    <html><head></head><body id="body" bgcolor="white"><font face="Arial" size="2">This stuff is giving me a headache already!</font><br>Click to <a href="javascript:increaseFontSize();">increase font size</a> or <a href="javascript:changeText()">change text string</a> <script language="JavaScript"> // get to the <font> tag var fontObj = document.getElementById("body").childNodes[0]; // check the tag - returns "FONT" // alert(fontObj.nodeName); // check the type of node - returns 1 // alert(fontObj.nodeType); // get the text within the <font> tag var textObj = fontObj.childNodes[0]; // check the text value - returns "This stuff is giving me a headache already!" // alert(textObj.data); // check the type of node - returs 3 // alert(textObj.nodeType); function changeText() { // alter the node value textObj.data = "I need some aspirin. Now."; } function increaseFontSize() { // get the value of the "size" attribute of the node var size = fontObj.getAttribute("size"); // increase by 1 size += 1; // set the new value fontObj.setAttribute("size", size); } </script> </body> </html>

    I've used two different methods here. In order to alter the font size, I'vefirst used the getAttribute() method to return the current value of theattribute, and then used the setAttribute() method to write a new value.However, altering the text string is simply a matter of changing the valueof the text node's "data" property.

    There are a couple of things to keep in mind when using getAttribute() andsetAttribute(). All attribute names should be lowercase, and both names andvalues should be enclosed in quotes (if you omit the quotes, the valueswill be treated as variables). Obviously, you should only use attributeswhich are relevant to the tag under consideration - for example, you cannotuse a setAttribute("src") on a <font> tag.

    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 4 hosted by Hostway