I'm trying to stop a form from automatically submitting, then use JavaScript to replace the form with text telling the user the server is processing the form, and in the callback in my 'post' route on the server the user gets sent to the results page after some processing finishes with information from the form.
But before the span can appear in the HTML the form has already been submitted and the page is waiting for a response from the server.
If I insert a timeout function I can force the span element to appear before the form gets submitted, but there's got to be a better way to do this.
I tried using async await .then()
but that also didn't work - maybe loading the span can't finish rendering before the form is submitted? - Thanks
HTML and JS:
var proc = async () => {
var form = document.getElementById('form');
form.style.display = 'none';
var span = document.getElementById('testSpan');
span.style.display = 'block';
//using this timeout I can get the span element to appear
//window.setTimeout(() => {
//document.getElementById('form').submit()
//}, 500);
}
var btn = document.getElementById('submitBTN');
btn.addEventListener('click', async () => {
await proc().then(document.getElementById('form').submit());
})
<span style='display : none' id="testSpan">processing...</span>
<form method="POST" action="/submit-form" id="form" onsubmit="event.preventDefault();">
<input required id="linkInput" type="text" name="name" />
<input id='submitBTN' type="button" value="Submit"/>
</form>