0

I have a question about ajax. I have a project made in Laravel 7 that performs the functionality of adding products and increasing the quantity, and it has already been done. To avoid refreshing the browser, I did it through ajax and it works. I show my code

<script type="text/javascript">
$(document).ready(function(){
    $(".btn-update-quantity").on('blur', function(e){
        var id = $(this).data('id');
        var href =  '/admin/updateservicios';
        var cantidad = $("#quantity_"+ id).val();
        $.ajax({
            url: href + "/" + id + "/" + quantity,
            data: { 'id' : id, 'quantity':quantity },
            type: "GET",
            cache: false,
            success: function() {
                $("#table_refresh").load(href);
                //$("#table_refresh").load(" #table_refresh");
            }
        }); 
    }); 
});
</script>

What I do is by means of an input

 <input type="number" name="quantity" class="btn-update-quantity form-control" value="{{$part['quantity']}}" id="quantity_{{$servs_id}}" data-id="{{$servis_id}}">

every time an amount is entered, it is updated, but the problem starts when I send the first ajax request and if it changes the value and refreshes the div, but when I make another change in the amount, it no longer works. How could I do to send an ajax request again without refreshing the browser since it lost the functionality

JL DIAZ
  • 93
  • 7
  • Well you're replacing the element which has the event attached to it. So either reattach the event or use [Event Delegation](https://stackoverflow.com/questions/1687296/what-is-dom-event-delegation) -> TLDR: `$(document).on('blur',".btn-update-quantity",function(e){...}` – Reyno Nov 07 '22 at 16:25
  • thanks I will see how to implement the delegation – JL DIAZ Nov 07 '22 at 17:03

0 Answers0