I’m trying to delay the functionality of a Submit button for a few seconds after a page loads. That part I have working.
Where I’m stuck is I’d like an "error" message to appear if the button is clicked while it’s disabled. And then have the message disappear once the button is no longer disabled.
I’m having trouble getting this to work. My thought was to use a "click" event listener, which doesn’t work while the button id disable — which defeats the purpose.
Here’s the html
<input type="submit" class="submitbtnstyle" id="submitbnid" value="Next"></input>
<div id="button-error"></div>
Here’s the working disable/enable function…
window.onload = (submitDelay);
function submitDelay(){
document.getElementById("submitbnid").disabled = true;
setTimeout(function() {
document.getElementById("submitbnid").disabled = false;
}, 3000);
}
And here’s the error message that I haven’t quite figured how to make work with the disable button and/or combine with the above script.
document.getElementById("submitbnid").addEventListener("click", ButtonError);
function ButtonError(){
document.getElementById('button-error').innerHTML = 'Oops, Too Fast. Try Again in 2 seconds.';
setTimeout(function() {
document.getElementById('button-error').innerHTML = '';
}, 3000);
}
For reference, the purpose of this delay is because I have a hidden form field that dynamically updates the value after searching/finding an id in a first-party cookie that is set in the browser. There is a slight delay placed for this process as the cookie has to be first set prior to searching for and adding this id.
So perhaps a better strategy is not to arbitrarily disable the submit button for x seconds, but rather somehow check to see if the id is updated in the hidden field. But I’m not entirely sure how to go about that approach. So I’m resorting to a 3 second delay, which should not cause an issue for the majority of users.
window.onload = (submitDelay);
function submitDelay() {
document.getElementById("submitbnid").disabled = true;
setTimeout(function() {
document.getElementById("submitbnid").disabled = false;
}, 3000);
}
document.getElementById("submitbnid").addEventListener("click", ButtonError);
function ButtonError() {
document.getElementById('button-error').innerHTML = 'Oops, Too Fast. Try Again in 2 seconds.';
setTimeout(function() {
document.getElementById('button-error').innerHTML = '';
}, 3000);
}
<input type="submit" class="submitbtnstyle" id="submitbnid" value="Next"></input>
<div id="button-error"></div>
Edit: here is the hidden field that gets updated to a random letter/number string, where {clickid} is the default value:
<input type="hidden" name="clickid" value="{clickid}">
2
Answers
Per @Geshode advice, I did the following to check if the default value of the form field was changed. If not, then display message and prevent default behavior of submit button. Once a couple seconds passes after the error message, the button will work as normally and (I assume) will check again if the value has changed when the user hits Submit.
Thanks for the help and suggestions from all.
.
Because disabled elements won’t trigger any mouse events, this is a little bit finicky to achieve.
But it is still possible, here’s a workaround:
<span>