Explain Codes LogoExplain Codes Logo

How to disable submit button once it has been clicked?

javascript
form-validation
button-states
javascript-events
Anton ShumikhinbyAnton Shumikhin·Sep 25, 2024
TLDR

Here's how to hinder multiple form submissions using onclick and disable the button immediately after click:

<button onclick="this.disabled=true;this.value='Sending…';return false;">Submit</button>

This technique safeguards against repeated requests, adjusts the button label to signify the current process, and holds off the default form submission action.

Elaborate with grace: working states of a button

First off, beyond simply disabling the button, it's vital to manage diverse button states. This involves changing the button's display text to something like "Sending…" alluding to an action in progress cranking under the hood.

// Rumor has it, button has a fleeting career as a progress meter 👷 onClick="this.value='Sending…';this.disabled=true;"

Form data integrity: disabled doesn't mean forgotten

Beware! When disabling form elements, you could end up ghosting important data. Disabled elements tend to pull a disappearing act during form submission, so be sure to equip your button with hidden form fields to safeguard valuable data.

// Hidden input: A magician's sleeve pocket <input type="hidden" id="buttonData" name="buttonData" value="Submit">

Cross-browser compatibility: because diversity matters

Remember, not all browsers speak the same dialect of JavaScript or HTML. It's crucial to devise a solution that understands the lingual nuances across all target browsers. A solution that smoothly works on Chrome might crumble on Edge!

Handling the oops moments: submission failed

Now, what if the form submission goofed up? It would be a nice move to offer your user a retry after a pause, using setTimeout, to toggle the button back to the enabled state.

// Here's to second chances! setTimeout(()=> {button.disabled = false}, 3000);

Opt for onsubmit: a robust form handler

An optimized strategy to prohibit double submission is to leverage the form's onsubmit event. This approach provides a well-rounded solution, considering it can manage various types of form submissions, the Enter key press included.

// Whispering wisdom to form: 'Hold your horses!' form.onsubmit = function(){ submitBtn.disabled = true; };

Keep accessibility in sight: from UX lens

Communicating button states to assistive technology is just as essential. Visual cues that inform the user about the progress of form submission can go a long way in enhancing the overall user experience.

Be mindful of data loss: don't disable at a cost

Think twice before pulling the disable trigger. If disabling a button hampers data integrity in multi-step form submissions, consider handling the submission differently, say through JavaScript.

// Be the guard of Galaxy(form), if Infinity(Button) Stone needs protection form.onsubmit = function(e){e.preventDefault(); /*handle your way*/ };

Form validation: setting the stage right

By incorporating the required attribute, ensure that no field feels left out before the curtain call. Reveal the submit button only when every piece is in place.

Signs on the way: Submission progress

Play the savvy guide and illuminate the user’s path with indicators during the wait leading up to successful submission.

// High-Tech Lighthouse: Guiding lost (network) packets button.value = 'Sending... Hold Tight!';

Graceful handling of failure: Re-enable on error

A patron of second chances? Here's a tip. Reactivate the submit button if the original submission hit a roadblock.

// Phoenix Button: Rises from the ashes of failed submission catch(err) {button.disabled = false;}

Striding ahead with CSS: Advanced interactivity

For the ambitious explorers, deviating from the familiar territories, venture into the enchanting lands of CSS. The introduction of pointer-events, for instance, can uplift button interactions to an advanced level!

// Repels Over-ambitious Clicks like garlic to vampires button[disabled] { pointer-events: none; }