0

I am trying to reload the table on every successful quantity change and row deletion in add to cart using ajax. But the table is getting reloaded only once after that no change is being observed.

Here is the code:

<script type="text/javascript">
$(".update-cart").change(function (e) {
    e.preventDefault();
    var ele = $(this);
    $.ajax({
        url: '{{ route('update.cart') }}',
        method: "patch",
        data: {
            _token: '{{ csrf_token() }}', 
            id: ele.parents("tr").attr("data-id"), 
            quantity: ele.parents("tr").find(".quantity").val()
        },
        success: function (response) {
            $("#cart").load(" #cart");
            //alert("This is working");
        }
    });
});
$(".remove-from-cart").click(function (e) {
    e.preventDefault();
    var ele = $(this);
    if(confirm("Are you sure want to remove?")) {
        $.ajax({
            url: '{{ route('remove.from.cart') }}',
            method: "DELETE",
            data: {
                _token: '{{ csrf_token() }}', 
                id: ele.parents("tr").attr("data-id")
            },
            success: function (response) {
                $("#cart").load(" #cart");
            }
        });
    }
});

1 Answers1

0

Button id b1 can be clicked only once, id b2 can be live clicked.

function why(e) {
        e.preventDefault();
        alert("You clicked " + e.target.id);
        $("#myDiv").html('<button id="b1" type="button">Click Me!</button><button id="b2" class="button" type="button">Click Me!</button>');
    }
    $("#b1").click(why);
    $(document).on("click", ".button", why);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myDiv">
  <button id="b1" type="button">Click Me!</button><button id="b2" class="button" type="button">Click Me!</button>  
</div>
Miller Cy Chan
  • 897
  • 9
  • 19