NiDZ
NiDZ

Reputation: 93

Jquery key press event not triggered on enter

$("#username,#password").keypress(function(e)
    {
        //alert('');
        if(e.keyCode == 13)
        {
            signIn();
        }
    });

The keypress event not calling if the enter button is pressed.

Upvotes: 1

Views: 3351

Answers (5)

Fseee
Fseee

Reputation: 2631

Use keypress and which statement:

$("#username,#password").keypress(function(e) {
    if (e.which == 13) {
        //call here your function
    }
});

Upvotes: 0

Adil
Adil

Reputation: 148110

Try using keyUp event.

Live Demo

$("#username,#password").keyup(function(e){
        //alert('');
        if(e.keyCode == 13)
        {
            signIn();
        }
});

This is also working with keypress

Live Demo

$("#txt1").keypress(function(e) {

    if (e.keyCode == 13) {
        //signIn();
        alert("keypress");
    }
});​

Upvotes: 4

justnajm
justnajm

Reputation: 4534

Key down is more appropriate:

$("#username,#password").keydown(function(e){
        if(e.keyCode == 13)
        {
            signIn();
        }
});    

Upvotes: 2

RobinJ
RobinJ

Reputation: 5243

Are you sure it's not the signIn (); function that's faulty? Because this seems to work just fine; http://jsfiddle.net/vDkBs/1/

Upvotes: 0

pozs
pozs

Reputation: 36214

  1. Use e.which instead of e.keyCode because this is what jQuery guarantees to be on event
  2. Try e.preventDefault(), because without it (if they are in a form) the form submits
  3. Maybe the best would be listening on form's submit event

Upvotes: 0

Related Questions