0

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)

Mohammad Saberi
  • 12,864
  • 27
  • 75
  • 127
  • try using keyup instead of keydown. also check this http://stackoverflow.com/questions/302122/jquery-event-keypress-which-key-was-pressed – Raj Jan 15 '12 at 07:21

1 Answers1

1

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/

JohnP
  • 49,507
  • 13
  • 108
  • 140