💾elem.wrappedWith()

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

Last updated