Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:35:07+00:00 2026-06-06T19:35:07+00:00

Inside a form I have a button. What is the difference between when I

  • 0

Inside a form I have a button. What is the difference between when I submit the form via JavaScript like this

<button onclick="document.forms[0].submit();">

and when I submit it like this

<button type="submit"></button>?

The first one works for me with most browsers except webkit-based. The latter works fine as well. No difference in functionality is apparent to me. Is there one?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:35:10+00:00Added an answer on June 6, 2026 at 7:35 pm

    The first example:

    <button onclick="document.forms[0].submit();">
    

    …will do two things:

    1. Its onclick will submit the first form in the document (i.e., the one specified by the 0 index in forms[0]).
    2. It will submit the form it is in (if it is in a form) because a button with no type attribute specified will be a submit button by default.

    This two-step double-submit behaviour can be seen in this quick-and-dirty demo: http://jsfiddle.net/fMwuX/ (and is likely to lead to weird behaviour that might be a bit confusing to debug). If the button isn’t actually in a form then this won’t be a problem.

    The second example:

    <button type="submit"></button>
    

    Will simply submit the form it is in (if it is in one).

    In my opinion the second option is definitely preferable for several reasons, including but not limited to:

    1. It will work even if the user has JS disabled.
    2. It doesn’t hard-code a form index with forms[0].
    3. It is shorter and clearer.
    4. It won’t clash with other form submit validation.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a button element inside a form like this: <form> <input type=text> <button>Test</button>
Inside asp.net form I have few dynamically generated buttons, all of this buttons submit
I have a form loading inside my page and there is a button out
I have an ASP.NET Webform and inside the form I have an input type
I have form in my page, I am using ajax.beginform(). Inside this form I
I have a div inside form something like <form> <div> showing some information here
I have a problem. I have form inside a fancybox. This form have a
I'm trying to have a clear button inside a html form, but it appears
I have a textarea inside a form (form action=post) and a link under this
I got this code working! I have a button inside a my UserControl dropped

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.