I'm trying to simply remove all nested li elements within ul, but it is not working. Why won't this snippet work? What am I missing?
html
<ul id="myul">
</ul>
<br/>
<button onclick="add();">add</button><br/>
<button onclick="remAll();">remove all</button>
js
function add()
{
$('#myul').append('<li>hello world</li>');
}
function remAll()
{
$('#myul').remove('li');
}
http://jsfiddle.net/gPnxY/
Do this instead, the selector argument for remove() is a filter for the object in question.
In this case the object in question is just the single element with the id myUl, therefore not containing any <li> elements.
jsFiddle
$('#myul').find('li').remove();
or
$('#myul li').remove();
jsFiddle here
This is nice:
$('#myul').empty();
Read more cool stuff: http://api.jquery.com/empty/
Also:
$('#myul').find('li').remove();
$('#myul li').remove();
$('#myul').contents().remove();
$('#myul').html('');
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