The createTextNode()
method creates a Text Node from the specified text.
createTextNode |
Yes | Yes | Yes | Yes | Yes |
document.createTextNode(text)
The required text
parameter is a String type value used as
the text of the Text node.
A Text Node object.
The following code shows how to create a text node.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<script>
function myFunction()<!-- ww w . java 2 s .co m-->
{
var t=document.createTextNode("Hello World");
document.body.appendChild(t);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to create a header.
<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<!--from www.j a v a 2s . c o m-->
<script>
function myFunction()
{
var h=document.createElement("H1");
var t=document.createTextNode("Hello World");
h.appendChild(t);
document.body.appendChild(h);
}
</script>
</body>
</html>
The code above is rendered as follows:
The following code shows how to create a <p> element and a text node, and append it to a <div> element.
<!DOCTYPE html>
<html>
<head>
</head><!--from w w w . ja v a 2 s .c o m-->
<body>
<div id="myDIV">A DIV element</div>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
var x = document.createElement("P");
var t = document.createTextNode("This is a paragraph.");
x.appendChild(t);
document.getElementById("myDIV").appendChild(x);
}
</script>
</body>
</html>
The code above is rendered as follows: