node.insertAdjacentText() code example

Example: HTML DOM insertAdjacentText() Method

<!DOCTYPE html>
<html>
<body>

<h1>HTML DOM insertAdjacentText() Method</h1>

<p>This method inserts a specified text, into a specified position.</p>

<h2 id="myH2">My Header</h2>

<p>Click the button to insert some text after the header:</p>

<button onclick="myFunction()">Insert text</button>

<script>
function myFunction() {
  var h = document.getElementById("myH2");
  h.insertAdjacentText("afterend", "My inserted text");
}
</script>

</body>
</html>

Tags:

Html Example