Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to submit a form by clicking enter and not via an existing submit button?

user-image
Question added by Besim Dauti , Ceo / Front End Developer , Nunforest
Date Posted: 2013/04/14

Basically, having a button of type "submit" in the form will handle the "enter" event without the need of any script.

wael kassis
by wael kassis , Tech Lead , NETNOVUS

I think you should go the Javascript route, or at least I would // Using jQuery.
$(function() { $('form').each(function() { $('input').keypress(function(e) { // Enter pressed? if(e.which == 10 || e.which == 13) { this.form.submit(); } }); $('input[type=submit]').hide(); }); });

Feras Abualrub
by Feras Abualrub , Web Solutions Manager , Qistas for Information Technology

use jquery to submit a form by serializing all inputs you want to submit.
the below code is an ajax , for you take the first line which takes all input values located in a function SubmitData() { var Data = $(".cs_slider").find('select').serialize() + '&'+ $(".cs_slider").find('input').serialize() + '&' + $(".cs_slider").find('textarea').serialize(); $.ajax({ url:'SubmitData.php', data: Data, type: 'POST', success: function(rData) { alert(rData); } }); }

Ankur Dadhich
by Ankur Dadhich , Senior subject matter expert , Amdocs

Dude, JQuery is the next thing that is used , the good old Javascript is gone out.
for your question use this....
code..............
You need to refer the latest jquery library over this block to work.
$(function() { Submitform(); }); functtion Submitform() { //Change #form to your form's ID $('#form input').keydown(function(e) { if (e.keyCode == 13) { $('#form').submit(); } }); }

Furqan Ul Karim
by Furqan Ul Karim , Senior Delivery Manager (Contract) , Virgin Red

I have recently created a form on the run-time and got it submitted without a need to click on a submit button.
Well this is not a magic just a normal javascript function i used for it.
there are 2 ways you can do it either you code the form to submit on body load event e.g.
see this below Hope it will help

Ashraf Sabry
by Ashraf Sabry , Freelancer developer , N/A

As Abdelhamid ABID pointed, a form with a button (submit or normal button) will submit on pressing Enter automatically without any JavaScript intervention.
If you want to remove the button and make Enter the only submission method, just catch the event on the FORM element (the onkeypress event bubbles from children to the form itself): $("form").on("keypress", function(e) { if(e.which == 13) this.submit(); }); Try it on JSFiddle http://jsfiddle.net/ashraf_sabry_m/hYeTf/1/

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.