forked from bgrins/devtools-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwrapelement.js
More file actions
29 lines (25 loc) · 743 Bytes
/
wrapelement.js
File metadata and controls
29 lines (25 loc) · 743 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
// 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);
}
}
})();