TheOnlyIdiot
TheOnlyIdiot

Reputation: 1202

How to trigger an event after hitting Tab key in textbox

I want to trigger an event like displaying an alert message when I hit the Tab key inside a textbox.

<input type="text" />

$("input").blur(function (e) {
   if (e.which == 9)
       alert("Hurray!!!");
});

What I want to happen is that whenever I type inside a textbox then hit Tab it will do something.

Im using jquery1.7.2.min.js

I really don't know if Im doing it right.

For the demo http://jsfiddle.net/QfCpC/

Upvotes: 4

Views: 34804

Answers (7)

Neel
Neel

Reputation: 11731

$(document).ready(function() {

    $("input").bind("keydown",function (e) {

   if (e.which == 9)        
       alert("Hurray!!!");
});
});

demo here..

http://jsfiddle.net/QfCpC/

Upvotes: 2

Rahul R.
Rahul R.

Reputation: 6461

<input type="text" />

$("input").keydown(function (e) {
   if (e.which == 9)
        $('#someButton).trigger('click');//or you can directly call the handler also
});

Upvotes: 2

Rab
Rab

Reputation: 35572

$("input").keydown(function (e) {

   if (e.which == 9)
       alert("Hurray!!!");
});

Fiddle Demo

Upvotes: 13

Sahil Grover
Sahil Grover

Reputation: 1915

The reason is when u hit 'tab' two actions takes place

  1. KeyUp for tab button
  2. Blur action for Input type field

Now according to your code you are adding eventlistner to blur event ... and blur event doesn't have property of giving you key binding.

So in order to do this you need to bind "keydown".

$("input").keydown(function (e) {
  if (e.which == 9)
       alert("YEYYYYYYY!!!");
});

Upvotes: 1

Anujith
Anujith

Reputation: 9370

Try : http://jsfiddle.net/cEzLL/

$("input").keydown(function (e) {
   if (e.keyCode === 9)
       alert("Hurray!!!");
});

Upvotes: 1

Moons
Moons

Reputation: 3854

Will this help

$("input").live("keydown" , function (e) {
if (e.which == 9)
   alert("Hurray!!!");
});

http://jsfiddle.net/QfCpC/3/

Upvotes: 2

Phil Bozak
Phil Bozak

Reputation: 2822

In order for the e.which parameter to be set properly, I believe it has to be called from a keydown event.

See the fiddle here. http://jsfiddle.net/QfCpC/2/

Upvotes: 1

Related Questions