tags:

views:

62

answers:

3

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.

+1  A: 

Try this:

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

Note: Using Jquery here.

Sarfraz
any eplanation guys????????????????????????
Sarfraz
Your initial posted solution does not work - you're including functions in a selector in a way that jQuery does not behave.
Erik
also mind that my answer is the oldest of all !!!!!!!!!!!!!!
Sarfraz
@Erik: please take your time to have a look at my answer again and respond accordintly, thanks.
Sarfraz
Removed the -1 since you removed the incorrect answer. :) Also, you should avoid recommending that people put handlers inline. While not 'wrong' its bad practice.
Erik
@Erik: Thanks man, you are right, i should not post inline solutions but i wanted to be little quick :)
Sarfraz
-1 For if the button is not under `li`, the parent of that button will still be removed and not a good idea... e.g. `div` with a button.
Reigel
@Reigel: i don't undertand what you mean and why :(
Sarfraz
Using this code, if there is another button on the form, it will work for any other button though it may not have to have the same function!!!
@user269431 You are correct, if the button happens to be in a form, and the form happens to be the parent, the form will be remove.
Reigel
@user269431 and @Reigel: You can easily modfiy the code with closest, etc but this was made to be simple. Thanks
Sarfraz
closest is still not the answer... you should be more clear on that to beginners... look for your code here http://jsbin.com/eciki
Reigel
+2  A: 
$("input[type=button]").click(function() { 
   $(this).parent().remove();
}
womp
Don't you mean `$('input[type="button"]')`, or `$('#list > li > input')`?
K Prime
Why yes I do. Thanks for the spot... my head was in my own project ;)
womp
How about $(":button")?
Bernard Chen
-1 For if the button is not under `li`, the parent of that button will still be removed and not a good idea... e.g. `div` with a `button`.
Reigel
@Reigel - and yet... all the buttons *are* under li's. You could easily change it to closest('li') or filter the result for 'li' - but I prefer to keep my answers straightforward and simple for obvious beginners.
womp
@womp Yes, but I think you should be explaining it for beginners... `closest()` is still not the answer... you might want to `$("#list :button").click()`
Reigel
A: 

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.

Reigel
if the list has an Id, how can we improve on the above code? In this case, the event will be launched on all anu list containing a button.
Also, I may have a button in the list that may have another event? The above example does not cater for a save button, that may create a list dynamically and add it as the last list.
hhmmm... you were not clear on that one... but if you can see, I already answered your question above.. How about clicking the check on the right, then ask another question on stackoverflow.
Reigel