jQuery remove all list items from an unordered list

Atticus picture Atticus · Aug 10, 2011 · Viewed 186.4k times · Source

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.

Answer

Richard Dalton picture Richard Dalton · Aug 10, 2011

$("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/