Skip to content Skip to sidebar Skip to footer

Wrap First Div Of A Div

I want to wrap or replace the first div and p which is in a div called entry-content : var outer = document.getElementById('.entry-content div'); outer.innerHTML = '
var parent = element.parentNode; var children = element.childNodes; var before = element.nextSibling; // reverse iteration always inserting the previous sibling before// the next onefor (var i = children.length - 1; i > -1; i--) { before = parent.insertBefore(children[i], before); } // remove the element parent.removeChild(element); }

Then all you have to do is pass a reference to the element you want to remove to the function.

DEMO

If you really want to replace the element though, then you just have to move all child nodes to the new element and replace the old element with the new one. I call the function "rewrap" here (because "replace" doesn't indicate that the content is copied):

function rewrap(element, new_element) {
    // append all the element's children to the new element
    while (element.firstChild) {
        new_element.appendChild(element.firstChild);
    }

    // replace old element with new
    element.parentNode.replaceChild(new_element, element);
}

DEMO

Post a Comment for "Wrap First Div Of A Div"