Sam
Sam

Reputation: 2347

making a submit button respond to hitting enter and click

I see on many sites that the enter key will submit a form but you could also click on the button to submit. Is there a trick to this? Or can a button have two different types of functions attached?

Upvotes: 5

Views: 3924

Answers (2)

Awais Qarni
Awais Qarni

Reputation: 18016

My dear there is nothing tricky. Just put the type of the button as submit and your will achieve what you are wanting. See This

After UPDATE

When there is a form tag in your html page, enter button always submit the form. If you have made your own button having type="button" even then you can submit form onclick it.

<input type="button" onclick="submitform();" value="Button">

and here is your javascript function

function submitform()
 {
   document.forms["myform"].submit();
 }

Hence your both cases can be achieved

Upvotes: 1

NJLaPrell
NJLaPrell

Reputation: 357

If you have a properly marked up form with an input element of type=submit as your submit button, the enter button should submit the form by default (when focused in an input element of type=text). Clicking works as well.

Any processing code would go on the form's "onsubmit" event instead of the button's "onclick" event. Additionally, you can return false to cancel the form submission (for example, if form validation fails).

Upvotes: 5

Related Questions