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

Introduction

Using the "afterbegin" value:

Click the button to insert a span inside the header:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This method inserts a specified text as HTML, into a specified position in the document.</p>

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

<script>
function myFunction() {//from   w  w  w.jav  a  2 s.c om
  var h = document.getElementById("myH2");
  h.insertAdjacentHTML("afterbegin", "<span style='color:red'>My span</span>");
}
</script>

</body>
</html>



PreviousNext

Related