Remove element from the DOM and List with jquery

1

I would like to delete the record that I upload with an img X, when I upload the file this is done well, showing a span with the name of the file in a label

"<span>"+urls[i].split('/').pop()+"<a id="+idMinuta+" href=\"javascript:EliminaRegistro();\"><img src=\"/sites/gadt/direcciones/dc/contabilidad/fape/SiteAssets/img/btn-borrar.png\" ></a>";

to be able to delete the registry I need the id, which is in <a> try to put a click event on the $ (document) .ready, but it does not work for me.

 $("img").click( function (event) {
                 alert();
    });

the DeleteRegistration () function; just call the definitive function that deletes the registration

    
asked by elsa 04.10.2016 в 16:22
source

3 answers

1

You can use:

$("img").click( function (event) {
    $(this).parent().parent().remove();
});

This what it does is to look for the container of the container of the 'img' that has been clicked and to eliminate it. In your case, it will eliminate the element.

    
answered by 04.10.2016 в 16:47
0

Add the eliminaRegistro parameter to the event function:

<a onclick="return eliminaRegistro(event)">...</a>

In your function, get the father of the anchor (guide yourself from your previous question if you still do not get used to Event ):

function eliminaRegistro(ev) {
  ev.target.parentNode.remove();
}
  • ev.target : the anchor
  • .parentNode : the father of the anchor, that is, the span .
  • .remove() : function that HTML elements have for auto to be removed from the DOM.

Or do it directly via jQuery:

$('.elimina-registro').on('click', function(e) {
  $(this).parent().remove();
}

Where .elimina-registro are the anchors to delete the records (assuming you have several), because if you do it for id and all have the same id you will not have the expected behavior.

    
answered by 04.10.2016 в 16:58
0

You can use remove to remove items from DOM and parent to access the parent element that contains it (thus removing SPAN) Also remember to access the attributes of an element of the sun you can do it with attr

$(function(){ 
   $('span a').click(function(e) {
 $(this).parent('span').remove();
 var id = $(this).attr('id');
 alert(id);
   });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<span>
    <a  class="btn btn-primary" href="#">Eliminar</a>
    <img src="assets/img/im1.jpg" alt="" width="50" height="50">
  </span>
  

$ (this) is the same as $ ('span a'), since it is the element that caused the click event.

    
answered by 04.10.2016 в 17:15