javascript - Remove button in table row ← (PHP, JavaScript, JQuery)

I am working on a shopping cart application. I have used jQuery to fetch the items and display it in table format. Each item table contains a remove button to remove it from the cart. I have used the click function on the table to remove the item. But if I click on the qty field also the table is removed because its inside the table. I want the table to be removed when I click the remove button inside the table.

$(document).ready(function(){
    $(".add_cart").click(function(){
        var id = this.id;
        $(this).prop("disabled",true);      
        $.get("cart.php",{
            "id":id
        },function(data){
            $("#sam").append("<table id='"+id+"' class='tables'><tr><td>"+data+"</td><td><input class='remove' id='"+id+"' type='button' value='Remove'></td></tr></table><br>");    
            $(".remove").click(function(){
                $(table.tables).remove();
                $("#"+id).prop("disabled",false);
            });
        });
    });
});

Answer



Solution:

use this:

$("table.tables").remove();

Answer



Solution:

Try

$(document).ready(function () {
    $(".add_cart").click(function () {
        var id = this.id;
        $(this).prop("disabled", true);

        $.get("cart.php", {
            "id": id
        }, function (data) {
            $("#sam").append("<table id='" + id + "' class='tables'><tr><td>" + data + "</td><td><input class='remove' type='button' value='Remove'></td></tr></table><br>");
        });
    });
    //use event delegation
    $("#sam").on('click', '.remove', function () {
        //get the table which contains the button
        var $tbl = $(this).closest('tr').remove();
        //get the id of the table instead of the id variable
        $("#" + $tbl.attr('id')).prop("disabled", false);
    });
});

Note that the Id of an element must be unique, you have a table and a button with the same id... remove the id from the button

Source