# elem.wrappedWithHTML()

{% tabs %}
{% tab title="💾 程式" %}
* replit: [toc](https://replit.com/@pegasusroe/toc#script.js)
* ⬆️ 需要：[str.htmltoelement](../../../../../js/val/prim/str/method/str.htmltoelement "mention"),

```javascript
/**
 * 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 ⭐ 
};
```
{% endtab %}

{% tab title="⬇️ 應用" %}
* create [table-of-contents](../../../create-insert-delete-nodes/table-of-contents "mention") for HTML document.
{% endtab %}
{% endtabs %}
