Javascript DOM How to - Add text node to an Element








Question

We would like to know how to add text node to an Element.

Answer


<!DOCTYPE html>
<html>
<head>
</head><!--from w w w .j ava  2s .  c  o m-->
<body>
  <script type="text/javascript">
function append()
{
    var node = document.getElementById("history");
    node.appendChild(document.createTextNode("text"));
}
window.onload = append;
</script>
  <div id="history"></div>
</body>
</html>

The code above is rendered as follows: