usertest
usertest

Reputation: 27638

Event to remove element before

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>

Upvotes: 0

Views: 2698

Answers (2)

Naeem Sarfraz
Naeem Sarfraz

Reputation: 7430

$('.divouter a').click(function(){
    var prevParent = $(this).parent().prev();
    if (prevParent.length) prevParent.remove();
});

Upvotes: 1

Gumbo
Gumbo

Reputation: 655489

Try this:

$(".divouter a").click(function() {
    $(this).parent(".divouter").prev(".divouter:last").remove();
});

Upvotes: 1

Related Questions