Event to remove element before
-
20-09-2019 - |
Question
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>
Solution
Try this:
$(".divouter a").click(function() {
$(this).parent(".divouter").prev(".divouter:last").remove();
});
OTHER TIPS
$('.divouter a').click(function(){
var prevParent = $(this).parent().prev();
if (prevParent.length) prevParent.remove();
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow