JQuery remove () not working properly

I am making a simple FileManager, but I don’t know what is wrong with my code, I used several times functions remove()and / or detach()jQuery, but this time the selected node is not deleted from the rest of the grid. The code is as follows:

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
    $(".excluir").click(function(){
        $(this).remove();
    });

    $("#novo").click(function(){
        $("#gradearquivos").append("<div class=\"miniatura\"><button class=\"excluir\">X</button></div>");
    });
});
</script>

append()works correctly by creating a thumbs up file, but remove()no! This time I was wrong? PS: jsFiddle Thanks, advanced!

+3
source share
2 answers

, . . parent('.miniatura'), , .miniatura div

<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
    $("#gradearquivos").on('click',".excluir",function(){
        $(this).parent('.miniatura').remove();
    });

    $("#novo").click(function(){
        $("#gradearquivos").append("<div class=\"miniatura\"><button class=\"excluir\">X</button></div>");
    });
});
</script>

+2

, jQuery on() . , excluir, javascript DOM . , click . , , miniatura

Live Demo

$(document).on("click", ".excluir", function(){
    $(this).parent('.miniatura').remove();
});

, , . , , , , , jQuery doc.

+1

All Articles