Convert HTML raw string to DOM Nodes in Javascript

To Convert HTML raw string to DOM Nodes in Javascript, we just use createContextualFragment method like this:




Usage:

const buttonElement = htmlDom.querySelector('#button'); // Get its child
document.body.prepend(htmlDom) // or prepend it
// or do something you need


The createRange().createContextualFragment() method returns a DocumentFragment by invoking the HTML fragment parsing algorithm or the XML fragment parsing algorithm with the start of the range as the context node


© 2019 4codev

Created with love by Sil.