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

  • SEARCH
  • Home
  • 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 9231861
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:10:29+00:00 2026-06-18T06:10:29+00:00

I am creating a web-based experiment using Javascript / JQuery which requires users to

  • 0

I am creating a web-based experiment using Javascript / JQuery which requires users to give responses by pressing certain keys, but not others. If they press the wrong key, they are supposed to get an alert popup telling them that they pressed the wrong keys. The experiment should stop waiting for the next keypress while the alert is displayed, but go back to waiting once the alert is dismissed.

The problem I’m having is that if the alert is dismissed by pressing the ENTER key, it seems to be registered by the key event handler, thus triggering another alert (because ENTER isn’t one of the permitted keys), even though I thought I’d written it in such a way that the key event handler shouldn’t be called until after the alert is dismissed. My question is, how to ensure that an ENTER keypress used to dismiss the alert will not be received by the subsequently called event handler?

Here’s some code:

function waitForKeypress( permittedKeys, callbackFn ) {
    returnFn = function(e) {
        $(document).unbind('keyup',returnFn);
        if ( permittedKeys.indexOf( e.which )!=-1 ) {
            callbackFn( { "success": true, "value": e.which } );
        } else {
            callbackFn( { "success": false } );
        }
    }
    $(document).keyup( returnFn );
}

var callback = function(data) {
    if ( data.success ) {
        goDoOtherStuff();
    } else {
        alert( "error message" );
        waitForKeyPress( permittedKeys, callback );
    }
}

waitForKeyPress( permittedKeys, callback );

I should explain that the reason I THOUGHT that an ENTER keypress used to dismiss the alert would not be received by the event handler in waitForKeyPress is because (I thought) Javascript should halt execution as long as the alert is displayed, so that the waitForKeyPress call inside the callback function should not go off until the alert has been dismissed, which is to say, after the ENTER key has been released.

(My actual waitForKeyPress is more complicated than the above – it waits for PAIRS of key presses, with only certain pairs permitted, which is the reason why I needed to make it a separate function in the first place. I don’t think the stuff I removed is related to my problem, though.)

EDIT: some people suggested this might have to do with the event handler not being unbound properly. I’m sure this isn’t the issue. Here’s a much more concise example, where the event handler isn’t even bound at all until after the alert, but the same issue arises:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
</body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
alert( "Hello world" );
$(document).keyup( function() { alert( "Goodbye cruel world." ) } );
</script>
</html>

Pressing ENTER to dismiss Hello World will immediately call up Goodbye cruel world.

  • 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-18T06:10:31+00:00Added an answer on June 18, 2026 at 6:10 am

    unbind keyUp before showing the alert

    edit #1

    I tried the example code in your edit on Safari 6 OS X 10.7.

    You’re right.

    This happens because standard buttons, like tke “Ok” of the alert dialog, that have the key equivalent “Return”, are pressed (I mean the button) when a Return keydown occurr. In other words the dialog is dismissed as the user press return and doesn’t wait for Return to be released.

    So the user press “Return”, the dialog is dismissed, keyup is binded, the user releases “Return”, the event is fired.

    You may work around this behaviour in different ways.

    The easier one (if it fits your needs) is binding keydown instead of keyup

    alert( "Hello world" );
    $(document).keydown( function() { alert( "Goodbye cruel world." ) } );
    

    Otherwise you may check what key is pressed and ignore “Return”.

    Keep in mind that keydown is triggered also by pressing CMD (for example if the user wants to close the window with CMD-W), arrow keys (that normally allow user to scroll the window if content is bigger that the viewport), etc.

    So either if you stck with keyup or switch to keydown some sort of filtering would be appropriate.

    Hope this helps

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

Sidebar

Related Questions

I'm creating a web based application (i.e. JavaScript with jQuery and lots of SVG)
I'm creating a simple web based game using the HTML5 canvas and JavaScript. I
I am creating a web application based on data using javascript. This data is
I'm creating a web-based mobile application using jQuery Mobile. I want to add +1
I am creating a web based email client using c# asp.net. What is confusing
I am creating a Web App based on JavaScript and HTML5 for content curation
I am creating a small web based tool that allows users to query a
I'm creating pages for a small web-based game which must have some background music
I'm working on creating a basic web based game using the HTML5 canvas and
I'm creating a web based application using Java Server Faces (JSF). I'm just wondering

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.