When not to use this.form.submit()

So ok, I’ve run into this problem in the past where I’m using an onclick event to submit a form from a button control. When you click the button you get a nice little Javascript error stating:

“this.form.submit is not a function”

Bugger…wtf?

This only seems to happen when you have a form element named “submit” already on your page, so the browser treats that “submit” element as an object which is of course NOT a function.

I seem to run into this when I want 2 ways of submitting the form as follows:

<form>
   ...
   <input type="submit" name="submit" id="submit" value="Submit Me" />
   <input type="button" name="submit2" id="submit2" value="Save Me" onclick="this.form.submit();" />
</form>

Note the name of the submit button is “submit”, hence causing an error :(

Make sure you don’t name your submit button as “submit” when you want to have multiples submits:

<form>
   ...
   <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit Me" />
   <input type="button" name="btnSave" id="btnSave" value="Save Me" onclick="this.form.submit();" />
</form>
Post a Comment or Leave a Trackback

16 Comments

  1. Vignesh
    February 8, 2007 at 10:37 am | Permalink

    Very useful information. I wasted some hours in identifying the solution by myself.

  2. September 27, 2007 at 12:09 pm | Permalink

    You rule man.
    Thanks you very much !

  3. shocker
    January 3, 2008 at 3:37 pm | Permalink

    you saved my day man!

  4. February 25, 2008 at 7:51 am | Permalink

    Thanks, you saved me a good deal of headache.

  5. Gary
    April 6, 2008 at 1:44 pm | Permalink

    Using Firefox, the button can be named ’submit’ and the JavaScript still works. The error appears to only be in IE.

  6. June 30, 2008 at 2:11 pm | Permalink

    Very useful tip, thanks!

  7. pike
    August 21, 2008 at 1:49 pm | Permalink

    doh :-) thanks, found this page quick enough.

    i *did* have this error in firefox, btw

  8. Ravi
    November 21, 2008 at 9:22 am | Permalink

    You Rock…
    Thanks man..keep it up

  9. nigel
    December 10, 2008 at 1:43 am | Permalink

    Thanks, you saved me a good deal of headache.

  10. troy
    January 21, 2009 at 11:58 pm | Permalink

    Muchas gracias. This was driving me crazy…

  11. Dave
    April 16, 2009 at 9:35 am | Permalink

    Nice short to-the-point solution! :)

    Thanks Michael!

  12. Alex
    September 10, 2009 at 6:37 pm | Permalink

    I second that the error does appear in Firefox, too. Thanks for the explanation, it did save me some troubleshooting

    Alex

  13. November 11, 2009 at 10:49 am | Permalink

    Thanks a million!

  14. Tommy
    January 21, 2010 at 10:09 pm | Permalink

    Saved me countless hours checking here within the first 5 minutes of searching. Thanks a lot!!

  15. Constantin Barb
    April 2, 2010 at 10:09 pm | Permalink

    A rule of almost any programming language:
    “Don’t name your variables, methods, objects or functins like the reserved words, wich, in most cases, are exactly the specific functions names.
    I have a simple solutions: I always put an “_” (underscore) in front of all my custom objects, and, in most programming languages, it’s saving me from headhecks.
    Like so, “_submit” is not an reserved word.

    Cheers from Romania!

  16. Wayne
    May 17, 2010 at 10:48 pm | Permalink

    Although not presently common practice, I have recently stumbled upon several apparently learned resources ( for instance, http://rickyrosario.com/blog/using-the-html-button-element-in-place-of-input-type-submit/ and http://cssglobe.com/post/7088/my-top-5-html-coding-preferences ) advising that BUTTON be used instead of INPUT for submission elements in forms.

    So, in deference to those “in the know,” I have decided to use BUTTON for all of my forms from now on. The thing is, I am not very good at JavaScript and want to know how to incorporate the snippet at http://www.mollerus.net/tom/blog/2008/01/preventing_multiple_page_requests_after_doubleclic.html into my BUTTON element. You see, I realize that while INPUT elements use self closing tags which contain the VALUE attribute, BUTTON elements, to the best of my knowledge, require separate opening and closing tags that enclose their value. With this in mind, how should I modify the snippet to prevent multiple form submissions? And if I decide to have multiple buttons in a form (unlikely), what about the IE6 bug alluded to in the first link?

    Thanks and sorry for being somewhat off-topic.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*