John
John

Reputation: 627

Use .unwrap to unwrap the children of all divs with a specified class

Is there a way to use the .unwrap function to unwrap all instances of a specific class (in this case 'blah')

<div class="blah">
  <a href="http://google.com">Google</a>
</div>

<div class="blah">
  <span>Testing</span>
</div>

Right now, I'm using a code like this:

$('.blah a').unwrap();

But this wouldn't unwrap the second piece of code with the span in it above.

Upvotes: 3

Views: 6702

Answers (2)

AaronAsAChimp
AaronAsAChimp

Reputation: 529

I found an issue with the accepted answer to this question. jQuery's .children() method doesn't return any of the text nodes. Meaning that any content under the div that is not wrapped in another element will get stripped.

This is my solution:

$('.blah').replaceWith(function () {
    return this.childNodes;
});

Upvotes: 5

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196002

How about

$('.blah').children().unwrap();

Demo at http://jsfiddle.net/gaby/KAzgq/

Upvotes: 11

Related Questions