Skip to content
Advertisement

Bootstrap modal on form submit not displaying at correct point

Requirement

I want to display a fixed message to the user whilst a file is being uploaded and attached to an email to discourage clicking back or refreshing the page or repeatedly clicking the submit button.

Background

I’m using bootstrap v3.3.7 and JQuery v3.3.1.

I thought a good way to tackle this requirement was to make use of the Bootstrap Modal plugin, especially when I realised you could fix the modal on screen using:

JavaScript

I have the following modal:

JavaScript

I have the following form:

JavaScript

I have the following button:

JavaScript

And the following jquery:

JavaScript

Issue

What I was hoping would happen is that the modal would appear once the unobtrusive validation has passed, and is displayed until the user is redirected to a thank you page.

What I’m finding happen is that the modal isn’t appearing until later in the process.

For example, if I put some alerts in to see what’s happening, using the code below, the modal doesn’t appear until after ‘here3’. It’s as though the .modal is firing an instruction to browser to display the modal but it doesn’t action it immediately.

JavaScript

Alternative attempt to achieve requirement

I’ve tried using an anchor instead of a submit button but again, the modal doesn’t appear until after ‘here3’:

JavaScript

Javascript function:

JavaScript

Question

How can I achieve my requirement? As per my comment in the Javascript function above, can I wrap the form submit in a promise or delegate or can I set something on the DOM to fire after the modal has displayed or is there an alternative to the Modal that I should look at?

Advertisement

Answer

In the end I got this working by using the following. Hopefully, someone else may find this useful:

Same button and form

JavaScript

Modal

Note the additional data-backdrop and data-keyboard attributes on the 1st div

JavaScript

jQuery

I ended up taking the key elements from the modal plugin in the Bootstrap js file and the modal appeared at the point I expected it to whilst still giving me the modal display effects that I was looking for.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement