Javascript DOM HTML Document createTextNode() Method append to document

Introduction

Create a text node:

var t = document.createTextNode("Hello World");

Click the button to create a Text Node.

View in separate window

<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">Test</button>
<script>
function myFunction() {/* w  ww  .  jav a 2s  .  co m*/
  var t = document.createTextNode("Hello World");
  document.body.appendChild(t);
}
</script>

</body>
</html>

HTML elements often consists of both an element node and a text node.

To create a header (e.g. <h1>), you must create both an <h1> element and a text node:




PreviousNext

Related