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?
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?
Share Improve this question asked Nov 3, 2011 at 14:28 SamSam 2,3479 gold badges39 silver badges53 bronze badges2 Answers
Reset to default 5If 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).
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