Reputation: 57176
How can I remove an element but not the content inside that element?
<a href="#">
<span>
<img src="pic-1.jpg"/>
</span>
</a>
I want to remove the span
that wraps the image.
So I can get,
<a href="#">
<img src="pic-1.jpg"/>
</a>
Upvotes: 17
Views: 18358
Reputation: 3240
$(document).ready(function(){
$span = $('span');
$span.replaceWith($span.html());
});
see example http://jsfiddle.net/vikastyagi87/Xaa39/6/
Upvotes: 10
Reputation: 53301
You'll have to modify your HTML architecture a bit here:
<a href="#" id="my_href">
<span id="my_span">
<img src="pic-1.jpg"/>
</span>
</a>
jQuery solution:
$("#my_href").html($("#my_span").html());
Non jQuery solution:
document.getElementById("my_href").innerHTML = document.getElementById("my_span").innerHTML;
Upvotes: 1
Reputation: 159105
The jQuery function unwrap()
is what you're looking for:
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
Check out the API doc page for more information.
Upvotes: 4