0

How can i submit form using Enter key

I am able to submit form using button click

Here is my code :

<form role="form" method="post" class="form-horizontal" id="leave_days">
 <div class="form-group">
 <label class="col-sm-2 control-label">Category Name</label>
 <div class="col-sm-9">
 <input type="text" name="txt_catname" id="txt_catename" class="form-control" placeholder="Enter Category Name" />
 </div> 
 </div> 
 <div class="col-sm-9">
 <button type="button" data-style="expand-right" id="btn_reg" class="btn btn-info ladda-button">
 </div>
</form>
Lukas
7,78420 gold badges80 silver badges127 bronze badges
asked Sep 18, 2015 at 7:32
0

5 Answers 5

3
<button type="button" data-style="expand-right" id="btn_reg" class="btn btn-info ladda-button">

type="button"will not submit a form on enter type="submit" so it will submit form on enter

 type="submit" 
answered Sep 18, 2015 at 7:36
Sign up to request clarification or add additional context in comments.

2 Comments

but also refresh page
@VijayAstound, if you don't want to refresh the page, you don't want to "submit" the form; you want to use Ajax to POST the contents of the form to the server. Using vanilla Javascript: developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest or using jQuery: api.jquery.com/jquery.ajax
1

Add a listener to your input to check for the correct keycode in Javascript. Notice the onkeydown-attribute added to your input

HTML:

<form role="form" method="post" class="form-horizontal" id="leave_days">
 <div class="form-group">
 <label class="col-sm-2 control-label">Category Name</label>
 <div class="col-sm-9">
 <input type="text" name="txt_catname" id="txt_catename" class="form-control" placeholder="Enter Category Name" onkeydown="checkForEnter(this, event);" />
 </div> 
 </div>
 <div class="col-sm-9">
 <button type="button" data-style="expand-right" id="btn_reg" class="btn btn-info ladda-button">Submit</button>
 </div>
</form>

On keycode 13, which is enter, get the form and submit it.

Javascript:

function checkForEnter(element, event) {
 if(event.keyCode == 13) // 13 is enter
 document.getElementById("leave_days").submit();
}

https://jsfiddle.net/8L5t119L/

OR just change your button to a submit input inside the form tags, like this:

<input type="submit" data-style="expand-right" id="btn_reg" class="btn btn-info ladda-button" value="Submit" />
answered Sep 18, 2015 at 7:40

2 Comments

i dont want to refresh page,in your code page refreshed
@VijayAstound, if you don't want to refresh the page, you don't want to "submit" the form; you want to use Ajax to POST the contents of the form to the server. Using vanilla Javascript: developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest or using jQuery: api.jquery.com/jquery.ajax
0

You can use jQuery for this:

<script type="text/javascript">
 $(document).ready(function () {
 var makeAllFormSubmitOnEnter = function () {
 $('form input, form select').live('keypress', function (e) {
 if (e.which && e.which == 13) {
 $(this).parents('form').submit();
 return false;
 } else {
 return true;
 }
 });
 };
 makeAllFormSubmitOnEnter();
 });
</script>
answered Sep 18, 2015 at 7:35

1 Comment

why he needs javascript ? i dont think there is any need to js for that
0

Add the following script in your code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
 $(document).ready(function () {
 $('#txt_catename').keypress(function (e) {
 if (e.which == 13) {
 $("#leave_days").submit();
 }
 });
 });
</script>
answered Sep 18, 2015 at 7:35

2 Comments

why he needs javascript ?
@NullPoiиteя, so even we have type="button" if we put some value in the text box and hit enter key so form won't get submitted.
0

finally i got answer

$("#leave_days").keyDown(function(e)){
 var fromto = $("#txt_catename").val();
 var key = e.which;
 if (key == 13) {
 if(fromto ==""){
 alert("Please Enter Category Name");
 document.getElementById('txt_catename').focus();
 return false;
 }
 if(fromto !=""){
 $.ajax({
 type : "post",
 url : "category-mgt/ajax/add-cat.php",
 data : "category="+fromto,
 success : function(data) {
 //alert(data); return false;
 if (data==1) { 
 alert('Category Added'); 
 $('#leave_days').trigger("reset");
 $('#dataTable').DataTable().ajax.reload(); 
 }else if(data==2){
 alert('Category Allready Exist'); 
 $('#leave_days').trigger("reset");
 } else {
 alert('something is going wrong please check this..');
 return false;
 //location.reload();
 }
 }
 });
 }
 return false;
 } 
 });
});
answered Sep 18, 2015 at 8:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.