<!DOCTYPE html>
<html lang="en-US">
<body>
<h2>Heading</h2>
<span style="color:blue;">Span</span>
<p>Click on the button to replace the span before the beginning of the h2 tag.</p>
<button onclick="myFunction()">Click Me</button>
<script>
var target = document.getElementsByTagName("h2")[0];
var elem = document.getElementsByTagName("span")[0];
function myFunction(){
target.insertAdjacentElement("beforebegin", elem);
}
</script> </body>
</html>