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 6695533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:13:59+00:00 2026-05-26T06:13:59+00:00

I am using the jQuery hotkeys plugin to bind some keypresses to events. I

  • 0

I am using the jQuery hotkeys plugin to bind some keypresses to events. I tried to change this to bind looping over an array instead, but it’s not working.

var letters = ["a","b","c"];
for (var x in letters)
{
    var letter = letters[x];
    $("el").bind('keydown', letter, function() { /*...*/  })
           .bind('keyup', letter, function() { /*...*/  });
}

This code binds all events to the last letter in the array (“c”) and none to others. Is there a better way of doing this ? Thanks a lot.

  • 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-05-26T06:13:59+00:00Added an answer on May 26, 2026 at 6:13 am

    Because JavaScript uses functional variable scoping.

    You want to scope letter in its own function:

    var letters = ["a","b","c"];
    letters.forEach(function(letter) {
        $("el").bind('keydown', letter, function() { /*...*/ } })
               .bind('keyup', letter, function() { /*...*/  });
    });
    

    Yours is basically a minor variation on the Infamous Loop Problem.

    See also closures.


    Based on your comments (some of which have been deleted?) I suggest the following approach:

    var events = {
        a: function() {
            console.log("a is for ALPHA");
        },
        b: function() {
            console.log("b is for BRAVO");
        },
        c: function() {
            console.log("c is for CHARLIE");
        }
    };
    jQuery("#el").keydown(function(e) {
          var ascii = e.keyCode || e.which;
          var handler = events[String.fromCharCode(ascii).toLowerCase()];
          if(handler) {
              handler();
          }
    });
    

    The jQuery keydown event executes for every key which the user presses down on – that second argument you’re passing to bind doesn’t constrain it to only one key.

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

Sidebar

Related Questions

Using this hotkeys plugin, https://github.com/jeresig/jquery.hotkeys , I'm having some problems with the following code:
Can I merge these in jQuery? I'm using the HotKeys plugin. $(document).bind('keydown', 'n', cycleNoise);
I'm using jQuery hotkeys plugin with the following code: $(document).bind('keydown', 'right', function(){ console.log('fired'); display_info();
Using jQuery 1.5.1 Tried this block of code in: head section as well as
I'm using jquery.hotkeys.js and mapping my keybinding in the following fashion: $(document).bind('keydown', 'i', function()
Using jQuery, how do you bind a click event to a table cell (below,
Using jQuery, what's the best way to automatically initialize a plugin on all current
Using jQuery and the Cycle plugin. Runs flawless in Safari, Chrome, Firefox, and the
Using jQuery, I'm trying to change the selected option in a SELECT element to
Using jquery how do i simply check if it is read only? this is

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.