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>
li
withid="div"
? – Wychelmdiv
ormyDiv
? – Wychelmul
an id ofmyDiv
is a very bad idea. EvenmyUL
isn't great, try to make it descriptive, not deliberately confusing. – Belgravia'#id'
looks pretty wrong, and doesn't match the HTML. Are you trying to mix jQuery into this? – Wychelmul
, so not what you want. To remove anli
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 theli
s removed. If you want to target just one, you'll need to specify it more clearly, by index, content, attribute -- something. – Wychelm