Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
142
rated 0 times [  148] [ 6]  / answers: 1 / hits: 34079  / 15 Years ago, mon, november 2, 2009, 12:00:00

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?


More From » jquery

 Answers
32

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;
});

[#98406] Wednesday, October 28, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yaquelina

Total Points: 517
Total Questions: 101
Total Answers: 96

Location: Egypt
Member since Tue, Jul 6, 2021
3 Years ago
;