Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

javascript insertBefore

<div id="parentElement">
  <span id="childElement">foo bar</span>
</div>

<script>
// Create a new, plain <span> element
let sp1 = document.createElement("span")

// Get the reference element
let sp2 = document.getElementById("childElement")
// Get the parent element
let parentDiv = sp2.parentNode

// Insert the new element into before sp2
parentDiv.insertBefore(sp1, sp2)
</script>
Source by developer.mozilla.org #
 
PREVIOUS NEXT
Tagged: #javascript #insertBefore
ADD COMMENT
Topic
Name
6+6 =