javascript - Remove button in table row


I am working on a shopping cart application. I have usedjQuery 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 theqty 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.

        var id =;
            $("#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>");    



use this:





$(document).ready(function () {
    $(".add_cart").click(function () {
        var 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

People are also looking for solutions to the problem: .htaccess - Rewrite url in htaccess php


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.