💾elem.wrappedWithHTML()

/**
 * wrap this Element with another Element using HTML string.
 * Note: this Element must be already in the DOM.
 * 
 * @example
 *
 *   let h2 = document.querySelector("h2");
 *   h2.wrappedWithHTML(`<a id="${anchorID}"></a>`);
 */
Element.prototype.wrappedWithHTML = function(html){
    const elem = html.htmlToElement();
    this.before(elem);    // insert element before self
    elem.append(this);    // move self into element ⭐ 
};

Last updated