Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove div using jquery?

Tags:

jquery

I have this code:

   <div class="container">
    <div class="row">
      <div class="col">
        <a href="1" class="delete_activity">x</a> 
      </div>
    </div>  
    <div class="row">
      <div class="col">
        <a href="2" class="delete_activity">x</a> 
      </div>
    </div>  
    <div class="row">
      <div class="col">
        <a href="3" class="delete_activity">x</a> 
      </div>
    </div>  
  </div>          

After the click on "delete_activity" I need to remove the "row" div that have that link. In the case the user click on X (href=1) i need to remove:

    <div class="row">
      <div class="col">
        <a href="1" class="delete_activity">x</a> 
      </div>
    </div>  

How can I do it with JQuery?

like image 731
Dail Avatar asked Apr 30 '26 09:04

Dail


2 Answers

The best way to do this is use closest().

$('.delete_activity').click(function(){
    $(this).closest('.row').remove();
});

Don't use parents (unless thats the behaviour you want).

closest will only remove the first element found up the DOM tree.

parents will remove ALL elements matched up the DOM tree.

like image 150
PeeHaa Avatar answered May 04 '26 02:05

PeeHaa


$('.delete_activity').click(function(){
    $(this).parents('.row').remove();
});

Documentation: http://api.jquery.com/remove/

like image 23
Damon Bauer Avatar answered May 04 '26 02:05

Damon Bauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!