We help IT Professionals succeed at work.
Get Started

JavaScript OnKeyPress Event Not Firing on Tab Key

P1ST0LPETE asked
Last Modified: 2012-05-09

I'm writing some JavaScript for a website I'm working on.

I have several text boxes that look similar to these:

A. <input type="text" id="R1C3" OnKeyPress="HideDropDownOnTabPress(event)" />

B. <input type="text" id="R1C4" OnKeyPress="return TextboxKeyPress(event, this)" />

And my JavaScript functions that handle the OnKeyPress events look like this:

function HideDropDownOnTabPress(event)
      var keyCode = (event.which) ? event.which : event.keyCode;
      if(keyCode == 9)

//Allows only numbers to be entered into textbox
function TextboxKeyPress(event, textbox)
      var keyCode = (event.which) ? event.which : event.keyCode;
    if ((keyCode > 31 && (keyCode < 48 || keyCode > 57)) || keyCode == 13)
        return false;
        return true;

The function TextBoxKeyPress() fires in the latest edition of all the major browsers (Firefox, IE8, Chrome, Safari, Opera) and works just fine.

The HideDropDownOnTabPress() function however, does not fire in all browsers.  It only works in Firefox and Opera.  Chrome, Safari, and IE8 just ignore it.

What am I doing wrong that prevents the HideDropDownOnTabPress() function from firing in Chrome Safari and IE?

What do I need to fix to get this to work.

Watch Question
This problem has been solved!
Unlock 2 Answers and 3 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE