开发者

Remove specific box with jquery

开发者 https://www.devze.com 2023-02-03 02:01 出处:网络
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.

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).

0

精彩评论

暂无评论...
验证码 换一张
取 消