spacer

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

home / experts / javascript / column44


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

 
Search
 


Developer News
Sir Tim Talks Up Linked Open Data Movement
From L.A. to Vegas With 100GbE
Salesforce Rolls Out Big Summer '08 Update

The createElement Method

The createElement method creates a stand-alone element under the document object. This element is a single node that has neither children nor father, and is not related to any other existing document nodes. The only information attached to such a node is the HTML tag it represents, such as <P>, <FONT>, or <TABLE>. The HTML tag is the only parameter you need to specify when calling the createElement. To create a <FONT> tag element, for example, you would write:

newObject = document.createElement("FONT");

A classic exercise for demonstrating the createElement method is to create an HTML table. It is popular for several reasons. First, the table includes quite a few different HTML tags. Then, the TABLE construct is deeply nested (five levels of hierarchy). Last, there is a hidden tag in HTML tables that you need to create explicitly (TBODY). All other HTML constructs do not have hidden tags. We have explained this feature in length in Column 40. Here is a script that will create all the different tags needed by a table:

tableObj = document.createElement("TABLE");
tbodyObj = document.createElement("TBODY");
trObj = document.createElement("TR");
tdObj = document.createElement("TD");

Notice again that these four nodes (tableObj, tbodyObj, trObj, and tdObj) are similar to four satellites in space. There is no connection (yet) or any other relationship between them. We'll show you later in this column how to connect between them and how to add them to document trees. The following script creates the above four tag nodes:

<HTML>
<HEAD>
<TITLE> DOM Demo </TITLE>
</HEAD>
<BODY ID="bodyNode">
<SCRIPT LANGUAGE="JavaScript">
<!--
tableObj = document.createElement("TABLE");
tbodyObj = document.createElement("TBODY");
trObj = document.createElement("TR");
tdObj = document.createElement("TD");
 // -->
</SCRIPT>
</BODY>
</HTML>

Run this script and see that the top level node (bodyNode) does not have any children, and thus:

bodyNode.firstChild = undefined

Also notice from the Alert window that the return value from the createNode method is the new object itself.

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 eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES
webref The latest from WebReference.com Browse >
Book Review: Head First JavaScript · Web Hosting Control Panels · Use Your Blog for Fast Search Engine Rankings
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
NetApp's Virtual Storage Strategy Crystallizes · F/MC Watch: A Cisco-Centric Approach · Olympic Time Trials Use Wi-Fi Mesh


Created: July 19, 1999
Revised: July 19, 1999

URL: http://www.webreference.com/js/column44/createtag.html