0

Whats the best way to disable a HTML Form Submit button when it is clicked and also change the text value on it?

ST3
  • 8,050
  • 2
  • 62
  • 85
user2710234
  • 2,737
  • 12
  • 32
  • 53

6 Answers6

3

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.

PlantTheIdea
  • 15,347
  • 4
  • 31
  • 40
1

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!

Brainarts
  • 81
  • 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
0

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.

Community
  • 1
  • 1
Ivaylo Strandjev
  • 64,309
  • 15
  • 111
  • 164
0

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...
}
ST3
  • 8,050
  • 2
  • 62
  • 85
  • ... and the inevitable jQuery reference happens haha. – PlantTheIdea Oct 10 '13 at 20:05
  • @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
  • @Plant I do not force to use that, just mentioned that as an option. – ST3 Oct 10 '13 at 20:18
  • 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
0

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

Jazzuzz
  • 340
  • 4
  • 11
0

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>
PRB1337
  • 1
  • 3