Run ❯
Get your
own
website
Result Size:
625 x 565
×
Change Orientation
Save Code
Change Theme, Dark/Light
Go to Spaces
<!DOCTYPE html> <html> <body> <h1>The Element Object</h1> <h2>The insertAdjacentElement() Method</h2> <button onclick="myFunction()">Move</button> <p>Click "Move" to insert the red span before the header:</p> <h2 id="myH2">My Header</h2> <p><span style="color:red">My span</span></p> <script> function myFunction() { const span = document.getElementsByTagName("span")[0]; const h2 = document.getElementById("myH2"); h2.insertAdjacentElement("beforebegin", span); } </script> </body> </html>