Reputation: 155
How can I delete a list based on the button clicked?
<ul id="list">
<li><input type ="button" name="Clear" value = "Clear1"/></li>
<li><input type ="button" name="Clear" value = "Clear2"/></li>
<li><input type ="button" name="Clear" value = "Clear3"/></li>
</ul>
e.g If I click clear2, it must remove the list it belongs.
Upvotes: 1
Views: 3967
Reputation: 65284
this should be the accurate answer:
$(document).ready(function(){
$("#list :button").click(function(){
$(this).parent().remove();
});
});
for if you use suggestions of womp's and Sarfraz's, you might be removing the wrong element.. You can see what I mean here.
Upvotes: 0
Reputation: 382899
Try this:
$(document).ready(function()
{
$("input[type=button]").click(function()
{
$(this).parent().remove();
});
});
Note: Using Jquery here.
Upvotes: 1
Reputation: 116987
$("input[type=button]").click(function() {
$(this).parent().remove();
}
Upvotes: 3