insertBefore
In this chapter you will learn:
Insert before
The following code inserts an Element into a Text Block.
<!DOCTYPE HTML> <!--from j ava 2 s . c om-->
<html>
<body>
<p id="textblock">
This is a test.
</p>
<p>
<button id="insert">Insert Element</button>
</p>
<script>
document.getElementById("insert").onclick = function() {
var textBlock = document.getElementById("textblock");
textBlock.firstChild.splitText(10);
var newText = textBlock.childNodes[1].splitText(4).previousSibling;
textBlock.insertBefore(document.createElement("b"),
newText).appendChild(newText);
}
</script>
</body>
</html>
Next chapter...
What you will learn in the next chapter: