💾str.htmlToElement()

/**
 * convert HTML into Element
 * 
 * @example
 *
 *   let div = `<div id="toc"></div>`.htmlToElement()
 *
 */
String.prototype.htmlToElement = function () {
    const template = document.createElement('template');
    template.innerHTML = this.trim();            // remove text node of whitespace
    return template.content.firstElementChild;   // return Element (no Text nodes)
};

Last updated