Javascript DOM HTML Element insertAdjacentText() Method with beforebegin as position

Introduction

Using the "beforebegin" value:

Click the button to insert some text before the header:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This method inserts a specified text, into a specified position.</p>

<h2 id="myH2">My Header</h2>
<button onclick="myFunction()">Insert text</button>

<script>
function myFunction() {//from  w w  w. j  a v  a2 s . c o  m
  var h = document.getElementById("myH2");
  h.insertAdjacentText("beforebegin", "My inserted text");
}
</script>

</body>
</html>



PreviousNext

Related