I’m trying to remove all elements below after clicking on the p element. Could you give me any pointers where am I going wrong?
<h2>Heading</h2>
<a href="#">
<p>Click me to delete everything</p>
</a>
$('p').click(function() {
$(this).remove().parent().remove().prev().remove();
});
Here is a jsfiddle.
Fiddle
andSelfdocs.addBack()is an alias forandSelfthat can also be used. The only difference is thataddBackis only available in jQuery 1.8+, whileandSelfis available since version 1.2.parent()andprev()are self-explanatory,andSelf()akaaddBacksimply adds the previous set of matched elements to the current set, that is, it combines.parent().prev()(the prev element of parent) with the previously executed.parent()(the parent element) and removes these in one go.