var div = document.createElement("div");
while(document.body.firstChild)
div.appendChild(document.body.firstChild);
document.body.appendChild(div);
or
var div = document.body.appendChild(document.createElement("div"));
while(document.body.firstChild !== div)
div.appendChild(document.body.firstChild);
And of course, you can turn them into a function by passing in the parent element and possibly the desired container as the name of the node or node.
function wrapInner(parent, wrapper) {
if (typeof wrapper === "string")
wrapper = document.createElement(wrapper);
var div = parent.appendChild(wrapper);
while(parent.firstChild !== wrapper)
wrapper.appendChild(parent.firstChild);
}
then
wrapInner(document.body, "div");
source
share