Data in Event Handlers
Pass data to event handlers via the second argument of .on(), accessible as event.data inside the handler.
Pass data to event handlers via the second argument of .on(), accessible as event.data inside the handler.
$(".delete-btn").on("click", { confirmText: "Really delete?" }, function(e) {
const msg = e.data.confirmText;
if (confirm(msg)) {
const id = $(this).data("id");
$.delete("/api/items/" + id);
}
});
e.data is set once when .on() is called — it does not update if the data object changes later.