Scriptbar Snippets 🚀
Handy and easy to understand snippets to keep in your browser
console-wrap-element.js

#Basic

Expose a method to wrap an html element with inputed element

Exposes a window function wrapElement(element, tag). The first parameter is either a DOM Node, or a selector string, and the second parameter is the tag name for the wrapping element. Implementation by gkatsev.

Wrap element method demo

// wrapelement.js // https://github.com/bgrins/devtools-snippets // Wrap a given element in a given type of element // wrapElement('.foo', 'h1'); // wrapElement(document.querySelector('#bar'), 'div'); // // LICENSE: [MIT](http://gkatsev.mit-license.org) (function () { window.wrapElement = function (el, whatToWrapIn) { var newParent = document.createElement(whatToWrapIn), oldParent, nextSibling; if (typeof el === 'string') { el = document.querySelector(el); } oldParent = el.parentNode; nextSibling = el.nextSibling; newParent.appendChild(el); if (nextSibling) { oldParent.insertBefore(newParent, nextSibling); } else { oldParent.appendChild(newParent); } } })();
Built with ❤️ by @domnguyen and everyone on the internet 🚀