Whats the best way to disable a HTML Form Submit button when it is clicked and also change the text value on it?
6 Answers
Someone recommended a Javascript onclick event, but I would instead recommend using onsubmit. This will be on the form itself rather than the button, and this encompasses the real event that you want to disable the button with (submission), as well as other events that could possibly trigger submit.
document.getElementById('FormId').addEventListener('submit',function(){
var button = document.getElementById('buttonId');
button.innerText = 'Something new!';
button.disabled = true;
});
Edited my answer to include the extra changes you were looking for (text of button, disabled as well).
Edit again: lets be super cross browser!
var form = document.getElementById('FormId');
function submitForm(){
var button = document.getElementById('ButtonId');
button.innerText = 'Something new!';
button.disabled = true;
}
if(form.addEventListener){
form.addEventListener('submit',submitForm,false);
} else {
form.attachEvent('onsubmit',submitForm);
}
This covers versions of IE prior to IE9. Obviously if you have more than one form you would try to make this a little more reusable, but this is the general gist.

- 16,061
- 5
- 35
- 40
-
agree, because "enter" key submits form as well – Peter Oct 10 '13 at 20:01
-
yup, exactly. not to mention the event triggered by javascript. – PlantTheIdea Oct 10 '13 at 20:01
-
technically speaking the question says `button when it is clicked` – Ivaylo Strandjev Oct 10 '13 at 20:01
-
haha i was just providing an alternative based on the intention of the action. – PlantTheIdea Oct 10 '13 at 20:02
-
I meant my comment to be a joke :) Sorry if you took it as a serious remark ;) – Ivaylo Strandjev Oct 10 '13 at 20:03
-
ah ... well played sir. i just never underestimate the trollishness of the average interwebber! :P – PlantTheIdea Oct 10 '13 at 20:04
The best way to achieve this is via JQuery.
Let say your form submit button has an id="submitButton". So add this script to you page head and refer the place where you put your downloaded JQuery and JQuery-UI script.
<script type="text/javascript" src="Scripts/jquery-2.0.3.js"></script>
<script type="text/javascript" src="Scripts/jquery-ui-1.10.3.js"></script>
<script>
function submitButton_OnClick() {
$("#submitButton").text("New Text");
$("#submitButton").attr("disabled", "disabled");
}
$(document).ready(function() {
$("#submitButton").click(submitButton_OnClick);
});
</script>
and this to your html
<button id="submitButton">test</button>
Check it work!

- 81
- 1
- 8
-
... and the inevitable jQuery-only answer. u even included jQuery UI for no reason whatsoever! haha awesome – PlantTheIdea Oct 10 '13 at 20:09
-
@PlantThaldea Instead of wining be creative... That kind of comment bring nothing to nobody... I think it must a site of Stuckup Exchange just for you somewhere... – Brainarts Oct 10 '13 at 20:10
-
my answer has been modified to show how to do this in pure Javascript. and i love jQuery, use it religiously, but it was in no way mentioned in this question. and what the hell is jQuery UI doing in there? it serves no purpose at all! this isnt about being stuck up, its about not proliferating bad coding habits. and the presumption on you ... "the best way to achieve this". really bro. – PlantTheIdea Oct 10 '13 at 20:14
You can add an onclick event and implement a javascript function to do what you want. You can have a look here to see how to disable a button from javascript. Also take a look here for an example how to change the text of the button using javascript.

- 1
- 1

- 69,226
- 18
- 123
- 176
You have to use JavaScript
and set on click event for example it should be:
HTML:
input type="button" value="value" id="1" onClick="OnClick(this.id)"/>
JavaScript:
function OnClick(id)
{
//some stuff...
}
Also you can use jQuery
and catch on submit, for example:
$("form").on('submit',function(event)
{
event.preventDefault();
//some stuff...
}

- 8,826
- 3
- 68
- 92
-
-
@Plant all the stuff that can be with jQuery can be done without that, however it guaranties code to work the same in all browsers. – ST3 Oct 10 '13 at 20:09
-
agreed ... but jQuery was never mentioned in the question, so why force him to use an entire library (and maybe learn it's syntax) for this function? – PlantTheIdea Oct 10 '13 at 20:17
-
-
fair enough, consider my scathe rescinded. :) i just am not a big fan of "jQuery is always the answer", so i probably overreacted when i saw that. – PlantTheIdea Oct 10 '13 at 20:20
You can use to onclick attribute to set the disabled attribute. Note that I don't cover how to reenable the button when form submission is complete.
<button type="submit" onclick="this.setAttribute('disabled', 'disabled');">Submit</button>
See the live example here at this fiddle:
http://jsfiddle.net/yinkadet/DPJGw/
I hope this helps

- 490
- 6
- 12
Here is my example of providing this functionality using Ruby on Rails and jQuery - it is an implementation example of the jQuery answer which i found very useful! (so there, haters):
<div class="col s12">
<div class="actions">
<%= f.submit nil, class: "waves-effect waves-light btn green darken-3", id: "gsb" %>
<script>
function submitButton_OnClick() {
$("#gsb").val("***CREATING***");
$("#gsb").attr("disabled", "disabled");
$("#new_group").submit();
}
$(document).ready(function() {
$("#gsb").click(submitButton_OnClick);
});
</script>
</div>
</div>

- 1
- 3