how to submit the form using ENTER key

282
April 05, 2017, at 7:11 PM

I am trying to design a cluster layout with input field and submit button..everything works fine when i type the text and click enter button..but i want to use the ENTER key for this purpose...

i mean ONE ACTION IN TWO BUTTON(ENTER key and SUBMIT button) below is what i have tried

$(document).ready(function(){
    $('editor').bind('keydown',function(event){
        var keyCode= (event.keyCode ? event.keyCode :
           (event.which ? event.which : event.charCode));
        if(keyCode==13){
            document.getElementById('update').click();
            return false;
        }else{
            return true;
        }
    });
});

for the above code,no action on keypress and no error in the console this is my entire code - https://pastebin.com/NbwEJQGX Can anyone suggest me some way to solve this issue???

Answer 1
    $("input").keyup(function(e){                            
       if(e.which == 13){ 
         $("#form").submit(); 
       } 
    });
Answer 2
$("input").keypress(function(event) {
    if (event.which == 13) {
        //do stuff
    }
});

See this code for button clicks

$( "button" ).click(function() {
    //do stuff for button click
});
Answer 3

I would suggest to add a class respective input field. then capture the event by the class as you may have multiple field where you want to submit the form after giving some value.

<input type="text" class="entr"/>
<input type="text" class="entr"/>
$('.entr').keypress(function(e){
    if (e.keyCode == 13) {
        $('#editorForm').submit();
    }
});
Answer 4

This should work

$(document).ready(function(){
     $(document).keypress(function(e) {
          if(e.which == 13) {
               $( /*  Your form here   */).submit();
               return false;
          }
     });
     $(/*    Where to click     */).click(function(){
           $( /*  Your form here   */).submit();
               return false;
           });
});
Answer 5

You can also call the function in the input

<input id="example" type="text" onkeypress="functionName(event, 0)" />
<input id="example" type="button" onclick="functionName(event, 1)" />
function functionName(e, n) {
if (e.keyCode == 13 || n == 1) {
   //do stuff
}
}
READ ALSO
Ajax img request end with success but cannot set image src

Ajax img request end with success but cannot set image src

I have a default image which is loaded in the HTMLThen i check if a personal image exists with an ajax request, the request is succesful however it cannot set the new image src

222
document.querySelector for name as css selector?

document.querySelector for name as css selector?

I know documentquerySelector('

264
Set image height and width accoding to quantity of image in single div

Set image height and width accoding to quantity of image in single div

i want to make image width 100% if div contain only one image but if more then one image then image is set according to item

204
What are invalid values when setting style properties in IE9?

What are invalid values when setting style properties in IE9?

Im doing a js with D3v4 libraryIt works in Chrome, Firefox, IE edge and IE 10

262