Java Reference
In-Depth Information
var newElem = document.createElement("h1");
newElem.appendChild(newText);
document.body.appendChild(newElem);
newText = document.createTextNode("This is some text in a paragraph");
newElem = document.createElement("p");
newElem.appendChild(newText);
document.body.appendChild(newElem);
</script>
</body>
</html>
Save this page as ch9 _ example3.html and open it in a browser (Figure 9-10).
figure 9-10  
It all looks a bit dull and tedious, doesn't it? And yes, you could have done this much more simply with
HTML. That isn't the point, though. The idea is that you use DOM properties and methods, accessed
with JavaScript, to insert these elements. The first two lines of the script block are used to define the
variables in your script, which are initialized to hold the text you want to insert into the page and the
HTML element you want to insert:
var newText = document.createTextNode("My Heading");
var newElem = document.createElement("h1");
Search WWH ::




Custom Search