MajAfy
MajAfy

Reputation: 3097

Removing higher layer

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

Answers (2)

Rory McCrossan
Rory McCrossan

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();
});

Example fiddle

Upvotes: 1

Sarfraz
Sarfraz

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

Working Example

Upvotes: 1

Related Questions