views:

24

answers:

2

How would I write an event in jQuery so that if I click any of the links it'll delete not the divouter surrounding it, but the divouter before it?

    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>
    <div class='divouter'>
<a href='#'>Link</a>
    </div>
+1  A: 

Try this:

$(".divouter a").click(function() {
    $(this).parent(".divouter").prev(".divouter:last").remove();
});
Gumbo
I didn't test your whole code but the prev method was just what I needed.
usertest
Is the `:last` filter required? `prev()` only selects one element, AFAIK.
J-P
I'm not sure, I haven't used it in my own code. I just used the prev method.
usertest
@J-P: What if the previous element is not one of *divouter*? Or can we ignore that case, @user201140?
Gumbo
A: 
$('.divouter a').click(function(){
    var prevParent = $(this).parent().prev();
    if (prevParent.length) prevParent.remove();
});
Naeem Sarfraz