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