AGIF AL AVIV AGIF AL AVIV - 4 months ago 10
Javascript Question

Cannot remove parent item

I'm trying to remove the parent item of an

<a>
, this is the html:

<div>123321<a class="del" rel="32" href="#">X</a></div>


and this is the code:

$(function()
{
$('.del').click(function(e)
{
$(e).parent().remove();
});
});


but nothing happen, why? JSFIDDLE.

Answer

(You could use this as mentioned in the other answer but also you have possibility to use e.target that refer to the current object also). so instead of passing event e you could pass the object of the current clicked element using e.target, check the example below.

$(function() {
    $('.del').on('click', function(e) {
        $(e.target).parent().remove();
    });
});
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<div>123321<a class="del" rel="32" href="#">X</a></div>