Javascript DOM How to - Insert HTML to body as a String








Question

We would like to know how to insert HTML to body as a String.

Answer


<!-- ww w.j  a va  2s  .c  om-->
<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>

var text = "Hello";
document.body.innerHTML = text;

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: