jQuery keyup ignores tab


Firstly, sorry I only have 195 points to spare :(

I've written some jQuery code to make a table editable.
All works well, except one really annoying point; I can't seem to capture the Tab key to move to the next field.
I can do this fine with the Enter key, but I'd rather use Tab as this is more expected behaviour.
(Especially as the Tab key is supposed to work, but I can't get it to!).

The important part of the code is shown below.

Thanks all for any suggestions!
$editField.keyup(function(event) {
            var key = event.keyCode;
            // the following line only works for CR, not TAB :(
            if (key == 13 || key == 9) {
                // return or tab key pressed

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Try using 11 also?

if (key == 13 || key == 9 || key == 11)

Or, you can try using the onblur function of the text object.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
And, finally, You can capture it from onkeydown event of the TextBox...

Regards. :)
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

bukkoAuthor Commented:
Thanks for the replies.

I tried key code 11, with the same result.
Using onblur might work, if I could be sure that the tab button caused the event to fire - how can I do that?
Keydown seems to behave the same as keyup, i.e. no improvement.

Thanks for the help so far. :)

bukkoAuthor Commented:
I think I know why this is.

As the jQuery API site says, "...the event is only sent to the element that has the focus."
Pressing tab gives the focus to the next available control!!

I think I'm going to have to bind keyup at document level so that it always fires.
Will try it and post results.
bukkoAuthor Commented:
Nope, tried binding to the document and the body; in both cases the tab didn't work but other keys did (including capslock!).

Please let me know if you think of something!

Steve KrileCommented:
I think the tab key is not captured on the Keyup event since keyup only looks for "characters".  I got this from a blog and it's helpful.

The answer I think is to use keydown() if you want to capture tabs.


In order to understand the difference between keydown and keypress, it is useful to understand the difference between a "character" and a "key". A "key" is a physical button on the computer's keyboard while a "character" is a symbol typed by pressing a button.  In theory, the keydown and keyup events represent keys being pressed or released, while the keypress event represents a character being typed. The implementation of the theory is not same in all browsers.
Hi, try this: (KeyDown must work!)

<input onkeydown = "alert(event.keyCode);">

bukkoAuthor Commented:
thanks, although that is really talking about keypress only capturing characters rather than keyup.
Keydown is in fact the better choice though because it fires before the default action, so the event is run from the original element, not the 'tabbed-to' one (if you see what I mean!)

that javascript works fine, but I need a jQuery equivalent.
The main problem is the firing order of events, because my tab key causes a new input field to be created on the table row below, which then has a 'focus' event.

I have managed to achieve what I need now, by using keydown and rejigging the code slightly.

senthurpandian was the first to point this out, so I'll award him the points.

Thanks all for your suggestions.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.