If you catch the click event from the button as follows:
$('#buttonId').off('click').click(function(clickEvent){
you can really prevent the closure of your modal. For this, depending on your situation, you will find these two functions useful:
clickEvent.preventDefault(); clickEvent.stopPropagation();
If I understand this site (which is in German) http://www.mediaevent.de/javascript/event-handler-default-verhindern.html correctly, preventDefault () stops the immediate action by default (for example, by reference). However, the event itself will still pass through the DOM and can be "heard" by various event listeners, one of which is an event listener that hides the modal. This requires a second function that stops the event from moving through the DOM. Thus, he cannot be heard by a hiding listener, and the window will not be closed (hidden). Therefore, I propose to implement such functions:
$('#buttonId').off('click').click(function(clickEvent){ //enter code here var myDataIsValid = true; // check if Data is valid => myDataIsValid = true or false if(myDataIsValid){ //do Stuff }else{ clickEvent.preventDefault(); clickEvent.stopPropagation(); //do other Stuff } });
In my code, I need to use stopPropagation (), since my default action is required, so you can use two functions independently.
Note. This solution has only been tested with Firefox.
source share