How to trigger an event after pressing Tab in a text field

I want to trigger an event similar to displaying a warning message when I press the Tab key inside the text box.

 <input type="text" /> $("input").blur(function (e) { if (e.which == 9) alert("Hurray!!!"); }); 

What I want is that whenever I type inside the text box and then press Tab , it will do something.

Im using jquery1.7.2.min.js

I really don't know if I am doing this correctly.

For a demonstration http://jsfiddle.net/QfCpC/

+4
source share
7 answers
 $("input").keydown(function (e) { if (e.which == 9) alert("Hurray!!!"); }); 

Demo Screenshot

+12
source

Will it help

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

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

+2
source

In order for the e.which parameter e.which be set correctly, I believe that it should be called from the keydown event.

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

+1
source

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

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

The reason is that when you click the tab, two actions are performed

  • KeyUp button for tabs
  • Blur action for Input Type field

Now, according to your code, you add an eventlistner to blur the event ... and the blur event has no property to give you a key binding.

So, for this you need to bind "keydown".

 $("input").keydown(function (e) { if (e.which == 9) alert("YEYYYYYYY!!!"); }); 
+1
source
 <input type="text" /> $("input").keydown(function (e) { if (e.which == 9) $('#someButton).trigger('click');//or you can directly call the handler also }); 
+1
source
 $(document).ready(function() { $("input").bind("keydown",function (e) { if (e.which == 9) alert("Hurray!!!"); }); }); 

demo here ..

http://jsfiddle.net/QfCpC/

+1
source

All Articles