Reputation: 6720
I forgot the jQuery command that will clear all list elements from a list. I did a bit of searching, done it a bunch of times before, but just simply forgot the command.
$("ul").clear()
$("ul").empty()
both didn't seem to accomplish this.. which command is it again?
UPDATE:
Thanks guys, I must have some syntax error on my selector.
Upvotes: 142
Views: 223046
Reputation: 1323
If you have multiple ul and want to empty specific ul then use id eg:
<ul id="randomName">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
$('#randomName').empty();
</script>
$('input').click(function() {
$('#randomName').empty()
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul id="randomName">
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<ul>
<li>4</li>
<li>5</li>
</ul>
<input type="button" value="click me" />
Upvotes: 4
Reputation: 35793
$("ul").empty()
works fine. Is there some other error?
$('input').click(function() {
$('ul').empty()
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
<li>test</li>
<li>test</li>
</ul>
<input type="button" value="click me" />
http://jsfiddle.net/infernalbadger/D5ss8/
Upvotes: 314
Reputation: 324
var ul = document.getElementById("yourElementId");
while (ul.firstChild)
ul.removeChild(ul.firstChild);
Upvotes: 3
Reputation: 11
Look your class or id. Perhaps Like This $("#resi_result").html(''); This should work:
Upvotes: 1
Reputation: 24053
$("ul").empty() should work and clear the childrens. you can see it here:
Upvotes: 3
Reputation: 253308
As noted by others, $('ul').empty()
works fine, as does:
$('ul li').remove();
Upvotes: 19
Reputation: 49188
An example using .remove()
:
<p>Remove LI's from list</p>
<ul>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
<li>Test</li>
</ul>
<p>END</p>
setTimeout(function(){$('ul li').remove();},1000);
http://jsfiddle.net/userdude/ZAd2Y/
Also, .empty()
should have worked.
Upvotes: 0