Reputation: 1202
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
Reputation: 11731
$(document).ready(function() {
$("input").bind("keydown",function (e) {
if (e.which == 9)
alert("Hurray!!!");
});
});
demo here..
Upvotes: 2
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
Reputation: 35572
$("input").keydown(function (e) {
if (e.which == 9)
alert("Hurray!!!");
});
Upvotes: 13
Reputation: 1915
The reason is when u hit 'tab' two actions takes place
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
Reputation: 9370
Try : http://jsfiddle.net/cEzLL/
$("input").keydown(function (e) {
if (e.keyCode === 9)
alert("Hurray!!!");
});
Upvotes: 1
Reputation: 3854
Will this help
$("input").live("keydown" , function (e) {
if (e.which == 9)
alert("Hurray!!!");
});
Upvotes: 2
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