Reputation: 3097
I want to remove 8th layer upper than this layer.
so I wrote this code : http://jsfiddle.net/3JaGg/ this code work now, but I want to know is there any better way ?
because I use parent()
for 8 times.
$(this).parent().parent().parent().parent().parent().parent().parent().parent().remove();
Note: I use many DIVs because I want rounding the corners with CSS2
Upvotes: 1
Views: 144
Reputation: 337714
Use closest()
, this will traverse up the DOM to find the nearest element with the selector you provide - in your case, closest(".popup")
will work.
$('.closeBtn').click(function() {
$(this).closest(".popup").remove();
});
Upvotes: 1
Reputation: 382881
Use closest()
method instead instead of multiple calls to parent()
.
Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
closest('.popup')
Upvotes: 1