Description
This tutorial will show you how to create new text nodes and add them to the document using DOM scripting.
The createTextNode method creates a new text node containing the specified text. This method returns a reference to the created text node.
reference = document.createTextNode(strText);
The method takes a single string parameter, strText, of the text to be created. The reference returned is a node object.
var oText = document.createTextNode("Hello World");
The reference returned by createTextNode is a node object referenced by variable "oText". However, the node has not yet been added to the document, it has only been created. To add to the document:
document.appendChild(oText);
Example
Replicate the HTML code you see below using DOM scripting.
<p>Hello World</p>
Solution:
var p = document.createElement("p");
var strText = document.createTextNode("Hello World");
p.appendChild(strText);
document.appendChild(p);

0 comments:
Post a Comment