I am trying to understand why my function is being executed before I get to the callback of my openModal script. I am fairly new to javascript but the understanding I had is that when you pass a function, it will execute when called (i.e., my callback() function in the openModal function). I've looked at a number of other examples and it appears I have the correct structure but I must be missing something. Any guidance here would be helpful.
Order of events when calling openModal
- List item
- executes callback function
- closes other modals
- opens the current modal
- does nothing because the callback function is undefined (why is it undefined?)
Shouldn't the events unfold like this
- closes other modals
- opens the current modal
- executes callback function
Any guidance here is appreciated.
<script>
function openModal(modal, callback) {
let modalCollection = document.getElementsByClassName(`modal`)
if (modalCollection) {
for (let index = 0; index < modalCollection.length; index++) {
closeModal(modalCollection[index]);
}
}
if (modal) {
modal.style.display = `block`;
}
if (callback) {
callback();
}
}
</script>
openModal(modalContactDelete, openModalContactDeleteCallback(${element.ContactId}))