30

Is there any way to remove the li elements of a ul without also removing the ul? I can only seem to find this.

var element = document.getElementById('myList');
element.parentNode.removeChild(element);

But, this removes the ul. I'm hoping to be able to remove and append li elements on the fly without also having to createElement the ul every time I remove li elements. Just looking for a simpler way. Thanks for any help.

<div id="listView">
  <ul id="myList" class="myList-class">
    <li>item 1</li>
    <li>item 2</li>
  </ul>
</div>
MAZUMA
  • 913
  • 3
  • 10
  • 20
  • You have an `li` with `id="div"`? –  Sep 13 '13 at 20:51
  • 3
    @Jeffman Keeps the next developer guessing. – user2246674 Sep 13 '13 at 20:51
  • Sorry, was in a hurry. html posted. – MAZUMA Sep 13 '13 at 20:55
  • Cough. So is it `div` or `myDiv`? –  Sep 13 '13 at 20:56
  • Wow, now that you actually posted your code, @Jeffman was right! Just so you know, giving a `ul` an id of `myDiv` is a _very_ bad idea. Even `myUL` isn't great, try to make it descriptive, not deliberately confusing. – Jordan Sep 13 '13 at 20:57
  • omg! it's a id. no div. – MAZUMA Sep 13 '13 at 20:57
  • `'#id'` looks pretty wrong, and doesn't match the HTML. Are you trying to mix jQuery into this? –  Sep 13 '13 at 20:57
  • Yeah. I agree on everything you guys are saying. The id's and class names should be better. They are not the real names. I just used placeholder names for the purpose of the question. The original post was bunk. Sorry for that. – MAZUMA Sep 13 '13 at 21:00
  • The code you have now DOES target the `ul`, so not what you want. To remove an `li` you need a reference to it. You get that by id or index number in some sort of collection. The answers below work if you want _all_ the `li`s removed. If you want to target just one, you'll need to specify it more clearly, by index, content, attribute -- something. –  Sep 13 '13 at 21:03
  • I know this removes the ul. I don't want to do that. I want to remove all li elements within the ul, while preserving the ul. What about Justin's method? Is this a proper way to handle? It seems pretty straight-forward and direct. – MAZUMA Sep 13 '13 at 21:06

7 Answers7

32

You can do something like this.

var myList = document.getElementById('myList');
myList.innerHTML = '';

If you are using jQuery

$('#myList').empty();

Both of these will remove EVERYTHING inside the list.

dr.Crow
  • 1,493
  • 14
  • 17
Justin Wood
  • 9,941
  • 2
  • 33
  • 46
17

This should do the trick:

var lis = document.querySelectorAll('#myList li');
for(var i=0; li=lis[i]; i++) {
    li.parentNode.removeChild(li);
}

Demo http://jsfiddle.net/krasimir/UhhuX/

Krasimir
  • 13,306
  • 3
  • 40
  • 55
  • 2
    This worked for me with one addition. Because I was only removing certain list objects, not all of them I had to add "i = i -1;" after the removeChild call to make sure the list and the i index stayed in sync. – Dowlers May 16 '16 at 19:40
  • I liked this answer because it only removes the `
  • ` elements. If, for some reason, you have something else in your `
      `, that won't get wiped out.
  • – Tony L. Mar 25 '18 at 21:37