There are a number of methods. You can write whole document, etc., but I think most flexible method is modification of properties of existing elements.
Here is the idea: you get some element by its
Id
and then assign some string to its property
innerHtml
.
For example:
<html>
<head>
<title>Writing HTML by JavaScript</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript"><!--
function writeHtml() {
var div = document.getElementById("placeholder");
div.innerHTML = "<h2>This is <i><big>generated</big></i> code!</h2>";
}
--></script>
</head>
<body>
<button onClick="writeHtml();">Try it!</button>
<div id="placeholder" />
</body>
</html>
—SA