-2

I just started using concepts in web development. I am trying to build a login page to take user's nick name and password and do something if they are matching in my data base. It is successful with button click(I mean if user press "SUBMIT" button) and results are as per expectation.fine!

My query is how to identify, if user press ENTER key after giving credentials instead of mouse click?

excuse me, if this query is duplicate. I am new to stackoverflow too! :) pls help.

-- Harsha.

Jonnix
  • 4,121
  • 1
  • 30
  • 31
sri
  • 91
  • 1
  • 10
  • Not to sound harsh, but did you try to google your question? – skywalker Nov 16 '15 at 12:31
  • instead of relying on the button being click, why not add a hidden field to your form and check for the existence of that instead – Dale Nov 16 '15 at 12:31
  • Yes I tried to find answer in google. Thanks. – sri Nov 16 '15 at 12:34
  • @Jai OP wants to detect if `form` is submited using `enter` key or clicking submit button, that's not really dupe. Now, it could be what he is looking for, depending what is expected behaviour – A. Wolff Nov 16 '15 at 12:35
  • What do you want to do? Prevent `form` to be submited if user press `enter` or what? Ask question regarding expected behevaiour, not workaround you think would fix it. See [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – A. Wolff Nov 16 '15 at 12:36
  • @A.Wolff - I want my form to be submitted in either case. click or enter. – sri Nov 16 '15 at 12:41
  • @sri So what is issue you have if enter is pressed? Why do you need to identify it? – A. Wolff Nov 16 '15 at 12:42
  • how could I tell my jquery to submit form without idetifying ENTER event? – sri Nov 16 '15 at 12:44

2 Answers2

1

You can use the .keypress where number 13 is the key ENTER, example:

    $(document).keypress(function(e) {
    if(e.which == 13) {
        alert('You pressed enter!');
    }
});
Ilanus
  • 6,690
  • 5
  • 13
  • 37
0

Use Key down and put the keycode of enter key as follows

$("#id").keydown(function (e) {
  if (e.keyCode == 13) {
   alert('hi')
  }
});
bhanu.cs
  • 1,345
  • 1
  • 11
  • 25