Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Apr 2008
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can someone help me with a function?

    I have this problem. I want to make it so a user can't click a button more than once on a web page, so I have javascript where the button gets disabled and the text of the button gets changed to "submitting..."

    Now, I made this by myself:
    <input type="submit" id="submitButton" onClick="this.disabled=true; submitButton.value='Submitting...';" />

    But I would really like it in a function so that on any button I can call the function like "disable(buttonId)" or something like that. So I was just wondering If someone can show me how I can write that function, or crank a quick one out for me? Thanks.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,210
    Thanks
    75
    Thanked 4,343 Times in 4,309 Posts
    Ummm...I think you already wrote the code. You just need to turn it into a function.

    Code:
    <script>
    function once(which)
    {
        which.disabled = true;
        which.value = "Submitting...";
    }
    </script>
    ...
    <input ... onclick="once(this);">
    ...
    Quite frankly, I don't see the need for a function for this, as more than likely you will want to use a different value for each button, to match its original value.

    Were you also asking how to attach that handler to all submit buttons??

  • #3
    New Coder
    Join Date
    Apr 2008
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hmm well thats all I really wanted. Thanks!

    But when i do
    <input type="submit" onClick="once(this)" />

    the nothing is submitted and the page is reloaded. Any tips?

  • #4
    Senior Coder ckeyrouz's Avatar
    Join Date
    Jun 2009
    Location
    Montreal, Canada
    Posts
    1,044
    Thanks
    5
    Thanked 179 Times in 179 Posts
    <input type="submit" onClick="once(this)" />
    let this code be like this:
    <input type="button" onClick="once(this);document.theform.submit()" />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •