Document.createElement()
to create a new element.Element.innerHTML
to set its inner HTML to the string supplied as the argument.ParentNode.firstElementChild
to return the element version of the string.const createElement = str => {
const el = document.createElement('div');
el.innerHTML = str;
return el.firstElementChild;
};
const el = createElement( `<div class="container"> <p>Hello!</p> </div>` ); console.log(el.className); // 'container'
Subscribe to get resources directly to your inbox. You won't receive any spam! ✌️