Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery unwrap removing the wrong containers

Obviously a syntax error on my part.

I've got a large quantity of divs where a small amount of them have content wrapped in an anchor. what I'm trying to do is remove the anchors.

thanks for your help!

http://jsfiddle.net/danielredwood/rg6n6/

Desired HTML:

<div class="box"><a href="#"><img src="#"></a></div>
// becomes:
<div class="box"><img src="#"></div>

JavaScript:

$('.box img').unwrap('<a>');
like image 353
technopeasant Avatar asked Feb 20 '26 22:02

technopeasant


2 Answers

Is this what you're looking for?

$('.box a img').unwrap()

.unwrap() has no arguments, it simply removes the parent tag of the matched selector. So by selecting the img tag directly under the a, you liberate it from its anchor, as you wanted.

like image 154
Morgon Avatar answered Feb 22 '26 11:02

Morgon


Try:


$('button').click(function(){
    $('.box a').contents().unwrap();
});

like image 33
Sudhir Bastakoti Avatar answered Feb 22 '26 13:02

Sudhir Bastakoti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!