I want to remove the span using jQuery,
I have tried the .unwrap();
but it's not working.
<div>
<ul>
<li><a href="#"><span>link</span></a></li>
<li><a href="#"><span>link</span></a></li>
</ul>
</div>
For HTML tags, you can press Alt+Enter and select Remove tag instead of removing an opening tag and then a closing tag.
jQuery uses: . append(); and . remove(); functions to accomplish this task. We could use these methods to append string or any other html or XML element and also remove string and other html or XML elements from the document.
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 to either move the function outside the ready handler (making it global), or, better, bind the click with jQuery: $(document). ready(function(){ $(this). mousedown(function(e){ var $x = e.
Obviously, unwrap
doesn't work as the span
s only have text nodes inside them and jquery doesn't handle text nodes too well... this works however (you could use also jQuery.text
instead of jQuery.html
if you're sure that the span
only contains text):
$('li a span').replaceWith($('li a span').html());
Working example
Edit: Actually, it seems that unwrap
works as well if you use jQuery.contents
to work around the jquery's inability to directly select text nodes:
$('li a span').contents().unwrap();
$('li').find('span').remove();
or
$('li').find('span').detach();
If you want to remove the wrapping only, try
var buffer = $('li').find('span').text();
$('li').find('span').parent().html(buffer);
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