Javascript DOM How to - Hard code the inner html








Question

We would like to know how to hard code the inner html.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--from w  w  w .  j  a  v  a2s  .co m-->
var tempDiv = document.createElement('div');
tempDiv.innerHTML = "<p><span>Hello</span></p>";
var result = tempDiv.getElementsByTagName("p")[0].innerHTML;
document.writeln(result);

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

The code above is rendered as follows: