Element.insertAdjacentHTML()
with a position of 'beforebegin'
to parse htmlString
and insert it before the start of el
.const insertBefore = (el, htmlString) =>
el.insertAdjacentHTML('beforebegin', htmlString);
insertBefore(document.getElementById('myId'), '<p>before</p>'); // <p>before</p> <div id="myId">...</div>
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️