Javascript DOM HTML Element insertAdjacentElement() Method with afterbegin as position

Introduction

Using the "afterbegin" value.

Click the button to insert the red span inside the header:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This method inserts a specified element, into a specified position.</p>
<span style="color:red">My span</span>
<h2 id="myH2">My Header</h2>

<button onclick="myFunction()">Move span</button>

<script>
function myFunction() {// www. ja  va2  s  . c o  m
  var s = document.getElementsByTagName("span")[0];
  var h = document.getElementById("myH2");
  h.insertAdjacentElement("afterbegin", s);
}
</script>

</body>
</html>



PreviousNext

Related