I have a table with some records in it and each of that records can be edited. I have a sidebar with edit form in it with the same inputs and the same submit button. When I try to execute the function on that button which will send an AJAX request, it executes as many times as many sidebars I opened before, when I need to update only that record which was actually edited.
That's the code:
// On Edit
$('.action-edit').on("click",function(e){
e.stopPropagation();
$(".add-new-data").addClass("show");
$(".overlay-bg").addClass("show");
const row = $(this).closest('td').parent('tr').first();
const agendaID = row.data('agenda_id');
const form = document.querySelector('#update_form');
const url = $(form).data('action_url').replace('.ID.', agendaID);
getAgendaInfo(agendaID);
$('.add-data-btn').on('click', function (e){
e.preventDefault();
console.log(agendaID); // displays IDs of all records where sidebar was opened.
const ajaxData = grabFormData();
editAgenda(url, row, ajaxData);
})
});