I'm having a strange issue adding the "ExecuteDelete(index)" function to an onclick attribute. The basic logic here is that when a user clicks delete, it triggers the Remove(index) function which shows a modal window and adds an onclick attribute to the Confirm Delete button on the modal. That confirm delete buttons onclick is supposed to execute after the Confirm Delete button has been clicked in the modal window.
This will work and the alert will be displayed after the user clicks the confirm delete button...
function Remove(index){
//set delete food modal message.
$("#DeleteFoodMessage").text("Are you sure you want to delete " + $("#FoodName-" + index).val());
//show modal.
$("#ConfirmDelete").modal();
//add onclick= event to the modal to delete the element at 'index'.
document.getElementById('ExecuteDeleteButton').onclick = ExecuteDelete;
}
function ExecuteDelete(index){
alert("This works");
}
However, when trying to pass in the parameter for the index, so that ExecuteDelete() knows what it's about to delete, the alert is called when the Remove() function is called.
function Remove(index){
//set delete food modal message.
$("#DeleteFoodMessage").text("Are you sure you want to delete " + $("#FoodName-" + index).val());
//show modal.
$("#ConfirmDelete").modal();
//add onclick= event to the modal to delete the element at 'index'.
document.getElementById('ExecuteDeleteButton').onclick = ExecuteDelete(index);
}
function ExecuteDelete(index){
alert("This does not work");
}
Thanks for the help.
-Andrew
ExecuteDelete(index); document.getElementById('ExecuteDeleteButton').onclick = undefined