¿Cómo eliminar div usando jquery?

Tengo este codigo:

   <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?

preguntado el 08 de noviembre de 11 a las 15:11

Have you actually tried? If so can we see what you produced? -

i think you also need to delete from database or only from HTML. bcoz on page refresh. u'll see it again if you do it only with jQuery -

5 Respuestas

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

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

No 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.

respondido 08 nov., 11:19

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

Documentación: http://api.jquery.com/remove/

respondido 08 nov., 11:19

this will remove the a tag only ! - Poelinca Dorin

@poelinca - how do you figure? - Damon Bauer

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

respondido 08 nov., 11:23

Pruebe lo siguiente

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

respondido 08 nov., 11:19

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

Use first to get the first parent above!

respondido 08 nov., 11:19

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.