Leem.fin
Leem.fin

Reputation: 42622

A simple case: detect keypress on a specific text field

I have two text input fields:

Name:<input id="name" type="text" value="" name="username">
Age:<input id="age" type="text" value="" name="userage">

When user input name and press "Enter" key, I would like an alert window pop up. When user input age and press "Enter" key, nothing happen.

To achieve above simple case described, I did the following jQuery code:

$("#name"),keypress(function(e) {
    if(e.keyCode == 13) {
        alert('You pressed enter on Name field!');
    }
})

You can also try the code out on jsfiddle link here.

However, when I input name and press enter, the alert window does not pop up, why?

Upvotes: 1

Views: 278

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230346

There's a typo. You put a comma instead of a dot

$("#name").keypress(function(e) {
    if(e.keyCode == 13) {
        alert('You pressed enter on Name field!');
    }
})

Here's a working jsfiddle.

Upvotes: 1

Related Questions