-3

I have the next code

$.ajax({
                url: '/data',
                type: "POST",
                data: JSON.stringify(formData),
                contentType: "application/json",
                dataType: "json",
                success: function(response){
                    for (var i=0; i<response.length; i++) {

                        var htmlEdit = "creating button here";
                        var btnEdit = jQuery(htmlEdit);
                        btnEdit.appendTo(divCollapse);
                        btnEdit.click(function() {
                            editBooking(btnEdit);
                        });
                    }
               }
});


function editBooking(btn) {
    btn.button('loading');
}

So I have rows with identical items. Buttons are shown as expected. Click to any button invokes loading state for the last button. What I'm doing wrong? Thank you.

Alex Zaitsev
  • 2,013
  • 4
  • 30
  • 56

1 Answers1

1

Just try that :

$.ajax({
    url: '/data',
    type: 'POST',
    data: JSON.stringify(formData),
    contentType: "application/json",
    dataType: "json",
    success: function(response){
        var htmlEdit;
        var btnEdit;
        for (var i=0; i<response.length; i++) {
            htmlEdit = 'creating button here';
            btnEdit = $(htmlEdit);
            btnEdit.appendTo(divCollapse);
            btnEdit.on('click', function(e) {
                editBooking($(this));
            });
        }
   }
});

function editBooking(btn) {
    btn.button('loading');
}
T'lash
  • 552
  • 1
  • 3
  • 15