Hi
Anyone who knows how to prevent a modal page /popup from closing when the user clicks outside the modal.
I have tried with this piece of code, but it doesn't do the job for me
Hi
Anyone who knows how to prevent a modal page /popup from closing when the user clicks outside the modal.
I have tried with this piece of code, but it doesn't do the job for me
Thanks Akshay
Ā
It works :)
Does this code not work anymore?
I have been using this line of code which worked really well with popup forms.
I havenāt touched the code since a year ago
The above code posted by Simon is correct, although āsceneā is the correct spelling, rather than āscheneā.
To just target a particular modal you can use the scene number, as shown on the below screenshot.
The code for this is:
//Prevents clicking off a Popup Modal
$(document).on(āknack-scene-render.scene_xxxā, function(event, scene) {
$(ā.kn-modal-bgā).off(āclickā);
});
(Change scene_xxx to your scene number, example from below would be āscene_19ā)
To set Globally for your whole App
//Prevents clicking off any Popup Modal
$(document).on(āknack-scene-render.anyā, function(event, scene) {
$(ā.kn-modal-bgā).off(āclickā);
});
Thanks Carl.
I actually have the global snippet of code you posted in my app.
Itās the same snipper of code Iāve been using since last year.
I am not too sure why it suddenly stopped working
Hi Jared,
I use this snippet in all my client apps and it works fine. If you have the same code there is no reason it wonāt work for you too.
Itās worth noting that if a modal dialogue box has page rules to show/hide fields, or fields, like a connected drop down that has filters, then it is possible to click outside the modal and it closes before all the rules have run. I think the JavaScript runs after the rules.
This has affected all apps with the piece of code.
Most of the pages donāt have rules on them.
Iāve even filled out a form which took a couple of minutes and then mistakenly clicked outside the dialogue box which resulted in the form closing and losing the entry.
But, if you are saying the code still works in all of your apps, then I will need to see where I went wrong with the JavaScript
Iāve pasted the code we use below
$(document).on(āknack-scene-render.anyā, function(event, scene) {
$(ā.kn-modal-bgā).off(āclickā);
});
Thanks for your prompt response Carl
Worth just checking this:
If itās not this then I would suggest that there is conflicting code? I am not a coder but I may be able to link you up with a colleague, although if this requires detailed investigation itās unlikely to be for free
Hi Carl,
I was able to locate the code conflicting with the popup form code.
Even though the form fully loaded and the code for the popup form was correct, there were pieces of code below that preventing the popup code from working.
This happened due to me trying to clean up unused code
Great news that you have found the conflicting code. Thanks for circling back on this one.
Does anyone know how to apply this to inline editing pop ups?
I can see in the source a couple of elements (drop-content, kn-popover) - but I canāt seem to see an equivalent background element to kn-modal-bg in the source to prevent the user clicking on it.
Thanks for the notes about the character change when working from a mac - that was spot on. Here is a paste of the code with the corrected characters for copy and paste from a mac.
$(document).on('knack-scene-render.any', function(event, scene) {
$('.kn-modal-bg').off('click');
});
Iād suggest this be an option when choosing modal from the UI!!! Canāt see why they wouldnāt option it rather than having to code it,
I am a bit stumped. I am using the exact code mentioned, but it is not working in my app. I have it as the first script loaded. Any ideas?
// Prevent Modals from Closing with a Click
$(document).on(āknack-scene-render.anyā, function(event, scene) {
$(ā.kn-modal-bgā).off(āclickā);
});
Looks ok to me? Iāve been using it for several years without issue
Would you all find it more useful if all modals just donāt close, or would you want the option for each scenario?
Would you all find it more useful if all modals just donāt close, or would you want the option for each scenario?
I personally think we need the choice.
Making it harder to close a modal can be appropriate when:
But there are other ways we use modals that donāt fit the above
Basically when you want to stay in the flow and return to your previous task, itās quicker to just click outside the modal to close it than focus your cursor on a small X icon IMO.
Hi @Kara - totally agree with Ian, allowing the user the option of either āpopupā or āmodal popupā would be my preference.
Just listened to the video and noticed I said the code goes into the CSS section, obviously I meant the JavaScript section
Providing options gives the builder flexibility. In this case modal or modeless, or perhaps as Carl says modal or popup.
Pardon my 2cents. But this seems like an easy feature to add that just makes sense, this isnāt a code rewrite and has been needed for years.
I think the dev team should build some real world apps in knack to feel the processes required. I add the above JS in every app because it drives my clients mad when accidentally click out the modal