spacer
Yehuda Shiran May 21, 2000
Creating Nodes under the DOM
Tips: May 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?

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 or any other relationship between them. 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>

Read in Column44, The Document Object Model (DOM), Part 5, how to connect these four isolated nodes.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business