Detecting tab press in javascript

I would like to have a text-area or a text input, that when the user presses tab (ie. Trys to go to the next form field), I can fire some JavaScript that opens up a hidden element.  Does anyone know a good way to do this?  Do I need to listening for any key-press on the page, or is there a way to do it specifically when they are only within the text-area.

Thanks in advance for the help.
LVL 11
Jeffrey Dake Senior Director of TechnologyAsked:
Who is Participating?
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.

Jan LouwerensSoftware EngineerCommented:
Ttry something like:

   $(document).delegate('.textAreaClass', 'keydown', function(event)
   {
      var keyCode = (event.keyCode ? event.keyCode : event.which);
      if (keyCode == '9')
      {
         event.preventDefault();
         alert('TAB');
      }
   });

Open in new window

0
Jan LouwerensSoftware EngineerCommented:
Actually, it looks like delegate has been deprecated. Instead, just use:
   $('.textAreaClass').on('keydown', function(event)
   {
      var keyCode = (event.keyCode ? event.keyCode : event.which);
      if (keyCode == '9')
      {
         event.preventDefault();
         alert('TAB');
      }
   });

Open in new window

0

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
Shaun VermaakTechnical Specialist/DeveloperCommented:
Here is an existing example that I altered a bit. TAB caught and displayed in the first textbox
http://jsfiddle.net/GaC7M/11/
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Jeffrey Dake Senior Director of TechnologyAuthor Commented:
Thanks guys.
0
leakim971PluritechnicianCommented:
please note : https://api.jquery.com/event.which/

The event.which property normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input.
0
Jeffrey Dake Senior Director of TechnologyAuthor Commented:
Thanks leakim971
0
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
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.