Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8985945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:26:11+00:00 2026-06-15T21:26:11+00:00

This question has probably been asked before, but I am not able to find

  • 0

This question has probably been asked before, but I am not able to find a suitable search phrase to locate it, either here or on the intertubes.

I have a web page for a game which uses the keyboard, so with jQuery I have set handlers in the document:

$(document).keydown(myKeydown);
$(document).keyup(myKeyup);
$(document).blur(myBlur);

But now any text written in an input field is stolen by the document event handlers, and characters only reach the input if they are not used in the document handlers. These handlers in essence look like this:

function myKeydown(event)
{
    if (event.which == 65)
    {
        console.log('a pressed');
        return false;
    }
    return true;
}

Now imagine that I have a text input box; if I press any key other than ‘a’ it shows in the input, but not ‘a’ (since myKeydown is “eating” it by returning false). I could return true in any case, but then if no input box is focused I get weird things like “search when you type”, if enabled.

I have tried to add handlers for the input box:

input.keydown(function() { console.log('down'); return false; });
input.keyup(function() { console.log('up'); return false; });
input.blur(function() { console.log('blur'); return false; });

With this code I get the console.logs, but of course keys don’t reach the box (because I am doing nothing with the presses). Can I send these events somewhere so the corresponding keys appear in the input box?

Note: some people have suggested to use event.stopPropagation() instead of return false. jQuery docs say:

Returning false from a handler is equivalent to calling both .preventDefault() and .stopPropagation() on the event object.”

That is precisely what I want: to prevent “search as you type” from kicking in, I have to prevent the default action, which is to type on an input and to “search as you type” on the page.

(Also, for some reason the keydown event handler does not have a stopPropagation() function on Firefox latest.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T21:26:12+00:00Added an answer on June 15, 2026 at 9:26 pm

    Two ways to prevent this.

    1. get the target in the event handler and ignore your code if the target is an input/textarea

    2. bind the same event to the inputs and stop propagation

    the first option is the right thing to do, IMO. Try:

    if ($(event.target).is('input, textarea, select'))
    {
        return true;
    }
    

    at the start of the event handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a question that has been asked before, but I couldn't find
this question has probably been asked before, but i'm stuck and i've tried a
I realize this question has probably been asked numerous times, but I have not
This has probably been asked before on SO, but I was unable to find
Sorry I know this question has probably been asked before, but do you know
This probably has been asked before but all I can find are questions regarding
This has probably been asked before, but a quick search only brought up the
I know this has probably been asked before but I can't find a specific
This has probably been asked before but I couldn't find the answer to my
This question has problably been asked before, but it seems like I can't phrase

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.