Mohammad Saberi
Mohammad Saberi

Reputation: 13166

why jQuery e.which does not work to detect input key?

I have a simple HTML like this:

Get alert with inserting a + sign: <input type="text" class="data" />

I want alert user whenever he/she press + key. But my jQuery code does not work:

$(document).ready(function(){
    $(".data").bind('keydown',function(e){
        if(e.which == 107){
            alert('Plus sign entered');
        });
    });
});

What do I have to do ?

(This is my jsFiddle)

Upvotes: 0

Views: 663

Answers (1)

JohnP
JohnP

Reputation: 50019

There were two things wrong in this code.

  1. There was a typo(?) - closing curly braces for the IF condition
  2. The key ID for SHIFT and = (which is +) is different from the numpad +

The following code fixes it

$(document).ready(function(){
    $(".data").bind('keydown',function(e){

        if(e.which == 107 || e.which == 187){
            alert('Plus sign entered');
        }
    });
});

Here's the fiddle - http://jsfiddle.net/DgUUB/

Upvotes: 1

Related Questions