Remove specific box with jquery
When you click Add a box. It adds a box with a deletlistbtn. Right now it removes all boxes with the same name/class/var. But i want the button to only delete the box/list its in. Can anyone tell me wich code i should use to accomplish that?
Right now i use this code;
$开发者_运维百科('.deletelistbtn').live('click', function() {
$(redbox).remove();
});
this is my entire code http://jsfiddle.net/XsCAN/
Use $(this).parent().remove()
or something similar. ($(this).parent().parent().remove()
- depending what are you going to accomplish)
Unless Im missing something I think you want
$('.deletelistbtn').live('click', function() {
$(this).parent().remove();
});
Just like the .removebutton
code.
http://jsfiddle.net/XsCAN/1/
While I realise that you've already accepted an answer, I feel it's worth pointing out that there is a slightly easier approach than navigating the DOM using ...parent().parent()...
, closest()
makes this easy by working upwards through the DOM automatically/natively to find the relevant element:
$('.deletelistbtn').live('click', function() {
$(this).closest('.redbox').remove();
});
JS Fiddle demo.
It does however work according to the element type, id
or class
(so .closest('.redbox')
works, .closest(redbox)
doesn't).
精彩评论