spacer

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

home / experts / javascript / column44


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

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

The createTextNode Method

The createTextNode method is very similar to the createElement method. Use the createTextNode method to create text nodes. Use the createElement method to create HTML tag nodes. The created text node is 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 its string value. This string is the only parameter you need to specify when calling the createTextNode:

newObject = document.createTextNode("This is an example string");

Let's prepare the text nodes for our table from Column 40. We need to prepare six nodes for the 3x2 table:

row1cell1Obj = document.createTextNode("This is row 1, cell 1");
row1cell2Obj = document.createTextNode("This is row 1, cell 2");
row2cell1Obj = document.createTextNode("This is row 2, cell 1");
row2cell2Obj = document.createTextNode("This is row 2, cell 2");
row3cell1Obj = document.createTextNode("This is row 3, cell 1");
row3cell2Obj = document.createTextNode("This is row 3, cell 2");

Notice that a new text node has neither children nor father. It is just a document "satellite" waiting to be adopted by a father or waiting to acquire some children. The following script demonstrates this node behavior after creation:

<HTML>
<HEAD>
<TITLE> DOM Demo </TITLE>
</HEAD>
<BODY ID="bodyNode">
<SCRIPT LANGUAGE="JavaScript">
<!--
row1cell1Obj = document.createTextNode("This is row 1, cell 1");
row1cell2Obj = document.createTextNode("This is row 1, cell 2");
row2cell1Obj = document.createTextNode("This is row 2, cell 1");
row2cell2Obj = document.createTextNode("This is row 2, cell 2");
row3cell1Obj = document.createTextNode("This is row 3, cell 1");
row3cell2Obj = document.createTextNode("This is row 3, cell 2");
alert( 
       "row1cell1Obj.firstChild = " + row1cell1Obj.firstChild + "\n" +
       "row1cell1Obj.nodeName = " + row1cell1Obj.nodeName 
      );
 // -->
</SCRIPT>
</BODY>
</HTML>

Run this script now. See that the new text node does not have any children. Also, notice that the returned value from the createTextNode is indeed an object.

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

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


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

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