user269431
user269431

Reputation: 155

remove <li> based on button click

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

Answers (3)

Reigel Gallarde
Reigel Gallarde

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

Sarfraz
Sarfraz

Reputation: 382899

Try this:

$(document).ready(function()
{
  $("input[type=button]").click(function()
  {
    $(this).parent().remove();
  });
});

Note: Using Jquery here.

Upvotes: 1

womp
womp

Reputation: 116987

$("input[type=button]").click(function() { 
   $(this).parent().remove();
}

Upvotes: 3

Related Questions