Matrym Matrym - 1 year ago 85
Javascript Question

How do you undo "surroundContents" in javascript?

I'm writing a script that needs to move a wrapping node element around on the page. I find that when I do this, I remove the previously wrapped children. How do I unnest the children of a node, so that I can move that parent node elsewhere?

I was thinking something like this:

var parg = document.getElementById("blah");

if (parg.hasChildNodes())
var children = parg.childNodes;
while (children.length > 0)

The line that I'm guessing is the problem is the "insertBefore" logic.

Answer Source

insertBefore operates on an element node and takes two arguments, the new node, and the node the new node will precede.

function unwrap(who){
 var pa= who.parentNode;
  pa.insertBefore(who.firstChild, who);



enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download