Detect the Enter key in a text input field

I’m trying to do a function if enter is pressed while on specific input.

What I’m I doing wrong?

$(document).keyup(function (e) {
    if ($(".input1").is(":focus") && (e.keyCode == 13)) {
        // Do something
    }
});

Is there a better way of doing this which would say, if enter pressed on .input1 do function?

11 Answers
11

Leave a Comment