jQuery remove all list items from an unordered list

JavascriptJqueryHtml Lists

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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

Solution 2 - Javascript

As noted by others, $('ul').empty() works fine, as does:

$('ul li').remove();

JS Fiddle demo.

Solution 3 - Javascript

This should work:

$("ul").html('')

Solution 4 - Javascript

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

Solution 5 - Javascript

$("ul").empty() should work and clear the childrens. you can see it here:

http://jsfiddle.net/ZKFA5/

Solution 6 - Javascript

   var ul = document.getElementById("yourElementId");

     while (ul.firstChild)
         ul.removeChild(ul.firstChild);

Solution 7 - Javascript

Look your class or id. Perhaps Like This $("#resi_result").html(''); This should work:

Solution 8 - Javascript

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.

Solution 9 - Javascript

this worked for me with minimal code

$(my_list).remove('li');

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAtticusView Question on Stackoverflow
Solution 1 - JavascriptRichard DaltonView Answer on Stackoverflow
Solution 2 - JavascriptDavid ThomasView Answer on Stackoverflow
Solution 3 - JavascriptRoccoC5View Answer on Stackoverflow
Solution 4 - Javascriptsparsh turkaneView Answer on Stackoverflow
Solution 5 - JavascriptNaorView Answer on Stackoverflow
Solution 6 - JavascriptAlexandre LimaView Answer on Stackoverflow
Solution 7 - JavascriptNanang RustiantoView Answer on Stackoverflow
Solution 8 - JavascriptJared FarrishView Answer on Stackoverflow
Solution 9 - Javascriptuser330844View Answer on Stackoverflow