Submit Button Enabling
"Enabling" you ask? Yes. We all know how to disable the submit upon form submission and the reasons for doing so, but what about re-enabling the submit button after an allotted amount of time. After all, what if the user presses the "stop" button immediately after submitting the form? They'd be screwed. Why not re-enable the submit button after an allotted amount of time so that the user may re-submit?
The MooTools JavaScript
window.addEvent('domready',function() {
var subber = $('submit');
subber.addEvent('click',function() {
subber.set('value','Submitting...').disabled = true;
(function() { subber.disabled = false; subber.set('value','Resubmit'); }).delay(10000); // how much time? 10 seconds
});
});
Of course, this isn't ideal in all situations. It is, however, a nice touch if your system can accommodate for it.
Update: Upon submission, the button's message changes to "submitting..." and once enabled, the message changes to "Resubmit." Thank you to Facundo Corradini for the suggestion!
Comments
Be Heard!
Share your thoughts without being a jerk! And wrap your code in <code> tags, f00!
Neat idea on the timeout. Though you shouldn’t run the same selector more than once, and should have cache the selector results for example, by writing (MooTools style):
var submit = $(‘submit’);
/* … */
submit.disabled = true;
@Lim Chee Aun: You always get me! Updated.
I’d prefer to turn the submit button to cancel button after 5 or 10 sec, so user still can choose cancel or wait. Nice touch david.
That’s thinking outside the box – I like it!
That was really good!
Neat little script David. You definitely don’t want the submit button to stay disabled if the form doesn’t finish submitting.
That was a really useful script.
I think it would be better if it give some feedback to the user, e.g. changing the button label to “submitting” or something like that.
Otherwise, it can be really confusing to some users.
Great suggestion Facundo Corradini! Updated.
Another touch you can possibly add is to change the cursor to default when in ‘Submitting’ state. It sort of gives the wrong impression that you can still click the button when in fact it’s disabled. Nice script though.
Hey David,
Nice thoughts on this script. I modified it slightly to use for my CMS, and (as usual) it works great in Firefox. In IE and Safari, however, it seems to cancel the actual form-sending. Here’s my code for use with multiple submit buttons; if anyone sees the problem(s) with it, feel free to use/fix it.
I triggered it by adding a “submit” class to the submits that would use it. Trying to have it find the ‘submit’ object would give errors when there were more than one submit button on the page.
$$(‘.submit’).each(function(o, x) {
o.addEvent(‘click’,function() {
var old = o.value;
o.set(‘value’,'Submitting…’).disabled = true;
(function() { o.disabled = false; o.set(‘value’,old); }).delay(10000); // how much time? 10 seconds
});
});
Not works with Mootools 1.11 :S
Nice trick but does’nt prevent people from continiously press the enter key.
… I finally saw “Prevent Early Form Submission by Hijacking the Enter Key” article that fits well for what I noticed just before.
Another solution may be a combination of this :
$(‘myform’).addEvent(‘submit’, function(e){
e.stop();
$(‘mytextinput’).addEvent(‘keydown’, function(e){
if(e.key == ‘enter’) {
e.stop();
}
});
//my code here …
//and at the end of the code …
$(‘identifiant’).removeEvents(‘keydown’);
});
By the way, why people never use the submit event on form ?
I find it more usefull than the click event on submit button because it also trigger the form on enter key press.
I just added this to an app, but it doesn’t seem to work in IE6 and 7.
When the submit button is disabled the form submission in canceled. Or am I missing something?
In order to get it work I need to stop the event and then programatic submit the form. See this page for example: http://dl.dropbox.com/u/3049118/lab/disable-submit-test.html
China TwoWin Stone fabricate the absolute black granite,slate,shanxi black granite,granite monuments,granite memorial,granite headstones,stone monument,grave markers,animal memorials,granite Tombstone,memorial stones
China TwoWin Stone Company is the granite manufacturers,factory and exporter in China,specialized in granite counter tops and marble vanity tops,granite tiles,granite and marble table tops,granite slabs and bath tub surround,Vanities and Sinks,granite stairs and marble mosaic tiles.