I have the following js code in the view:
//here I initialize two variables to temporaly store values
var id = 0;
var tr = "";
$(document).on("click", ".deleteItem", function(e) {
$('#deleteItemConfirmationModal .modal-body').html("Please confirm deleting this item");
$('#deleteItemConfirmationModal').modal('show');
// here I need two parameters (id and tr) to pass them into another function, so I save them into variables
id = $(this).data('id');
tr = $(this).closest("tr");
});
Then if a user confirms the item should be deleted. The code for this as follows:
$('#deleteItemConfirmationModal .modal-footer').on('click', function(event) {
var $button = $(event.target);
buttonId = $button[0].id;
if (buttonId === 'yesBtn') {
$.ajax({
url: '@Url.Action("Delete")',
type: 'POST',
data: { itemId: id }, // use first variable
success: function (data) {
if (data.IsOK) {
tr.remove(); // use second variable
}
}
});
}
});
The code works fine. But i don't think that it's a good practice to pass variables like this.
Can I use this approach or there is another one without using global variables?