Detect selected form when Enter key is pressed via jQuery or JavaScript

Ken Palmer

We have a web page with 2 forms that contain multiple fields. The first form has a customer billing address, and the second form has payment information. When a customer presses the Enter key, the billing address form is always submitted, regardless of whether the field that had focus was in the second payment information form.

The page needs to recognize the user's context, so that if a field in the second form has focus, and that person presses the Enter key, then that second form should submit, instead of the first form.

I am familiar with detecting the keypress event, and have used scripts like this to do so:

  detectEnterKey: function () {
      $('body').live('keyup', function (e) {
         if (e.keyCode == 13) {// 'enter' key 
            // Do something
         }
      });
      $('form').keypress(function (e) {
         if (e.which === 13) {
            // Do something
         }
      });
  },

What I don't know how to do, is detect the form context to know which form to submit. I suspect it may be available in the event (function(e)), but haven't seen an example of this. Thanks.

almo

You find the form that is parent of the field that has the focus when pressing enter and submit it.

$('.input').keyup(function(e) {
if(e.keyCode == 13) {
 $(this).parent().find('.form').submit();
}
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Submit a form only when 'enter' key is pressed

Form not submitting when enter key is pressed

jQuery: content to show when enter key is pressed

Call a javascript function when enter key is pressed

Changing input value inside a form tags when ENTER key is pressed

Call specific method in javascript when enter key pressed anywhere in page

Javascript : Scroll to top to the div when enter key is pressed

Trigger a button click with JavaScript when Enter key is pressed

Running a function if selected input & pressed enter key

Run javascript function when enter is pressed - JavaScript only no Jquery

Run Javascript when enter pressed

testing for ENTER key being pressed to submit a form

How to Detect When the Enter Key was Pressed using F sharp (F#)

Make a form run a javascript when ENTER is pressed or its button is pressed, without submitting

Xcode 9, Selected code snippet type removed when enter key is pressed

Force form to send certain submit button through (when there are multiple) when ENTER key pressed

Invoke a serie of actions when Enter key is pressed

How to show alert when enter key pressed

Submit a form when enter is pressed in a textarea in React?

Writing to a div from a form when enter is pressed

how get a row index from gridview windows form when enter key pressed

Is there any Javascript code to detect that no key is pressed?

Editor in not showing correct line number when enter key pressed continuously in JavaScript?

How do I detect when the delete key has been pressed with jQuery?

How to pass selected parameters to a form action url via jQuery or JavaScript

Javascript to trigger submit when enter is pressed

jQuery - detect if enter is pressed more than X times in X seconds

How to submit a form when the return key is pressed?

Enter key pressed fast