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>
Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.
You cannot remove an element from the DOM tree using CSS. You can only prevent it from being rendered in the layout with display: none ; doing so does not prevent it from responding to events or cause it to be ignored by CSS selectors such as + and :nth-child() .
Removing an element using the removeChild() method First, select the target element that you want to remove using DOM methods such as querySelector() . Then, select the parent element of the target element and use the removeChild() method.
You need unwrap
$('img').unwrap();
$(document).ready(function(){
$span = $('span');
$span.replaceWith($span.html());
});
see example http://jsfiddle.net/vikastyagi87/Xaa39/6/
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With