I have the code
<form id="myForm" method="POST" novalidate="">
<input type="text" id="NameInput" name="NameInput">
<button type="submit">Complete</button>
</form>
I want to show an alert like "The form has been submited!" and after clicking submit the form and complete the request
Add an event listener for the submit event that prevents the form from submitting by calling Event.preventDefault(), then use setTimeout to execute a function which alerts and submits the form programmatically after 2000 milliseconds:
myForm.addEventListener('submit', function(e) {
setTimeout(function() {
alert("The form has been submited!")
myForm.submit();
}, 2000)
e.preventDefault()
})
<form id="myForm" method="POST" novalidate="">
<input type="text" id="NameInput" name="NameInput">
<button type="submit">Complete</button>
</form>