How to trigger alert before closing modal

I have a form in a reveal modal popup that requires a fair bit of info. SO if the user clicks on the close button I want to just make an alert popup asking them if they are sure that they want to leave and that leaving would erase their data. I’m really not sure how to do this, any help or advice?

You can do this with onclick (.on('click')), event.preventDefault() and event.stopPropagation() to cancel the event from bubbling to the other listeners.

Thanks DanielRuf. I will give that a shot and see how I go