Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove <li></li> from ul?

how to remove a particular <li></li> from the ul list? i am adding it dynamically... and i need to remove it when the user click on clear. how can i do that?

like image 612
nimi Avatar asked Nov 02 '09 06:11

nimi


People also ask

How do I delete all Li in UL?

To remove all li elements from a ul element with JavaScript, we can set the innerHTML property of the ul element to an empty string. Then we write: document. getElementById("root").

How remove Li tag in jquery?

If you want to remove all <li> tags then $("#ulelementID"). html(""); will work.


1 Answers

That depends on what you have and what you're adding. Assuming you end up added items so it looks like this:

<ul id="list">
  <li><a href="#" class="clear">clear</a></li>
  <li><a href="#" class="clear">clear</a></li>
  <li><a href="#" class="clear">clear</a></li>
</ul>

then use remove() to remove a single item:

$("#list a.clear").click(function() {
  $(this).parent().remove();
  return false;
});

but if you have:

<ul id="list">
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>
<a href-"#" id="clear">Clear</a>

and you want to clear all of them then use empty():

$("#clear").click(function() {
  $("#list").empty();
  return false;
});
like image 155
cletus Avatar answered Oct 05 '22 14:10

cletus