1_2_3_Reapeater
1_2_3_Reapeater

Reputation: 276

Append a node's siblings using Cheerio?

I have a div containing an h3 and some divs. I want to essentially wrap all of the divs together in one container div, but Cheerio doesn't have a wrap or wrap-together function.

Right now I'm appending the container div, making it a sibling of the divs that need to be its children. But I'm at a loss as to how to move its siblings into this container. What I'd like to do is something like:

$("div.container").append($this.siblings("div"));

Essentially "append to this node, the div siblings of this node." But I can't figure out how to make the self reference.

Upvotes: 1

Views: 1137

Answers (1)

lxe
lxe

Reputation: 7619

You can always create a new wrapper and move all the div children from the original container to the new one:

// Create a wrapper
var newDiv = $('<div>');

// Move all divs from #container to the wrapper
newDiv.append($('#container').find('div'));

http://jsfiddle.net/5w93bs0f/

Upvotes: 5

Related Questions