In JQuery, how do I get the vaue of a variable based on keyboard input?

let's say I have a list of variables that reflect TV channels and their channel names, like so:

ch2 = "CBS"
ch3 = "ABC"
ch23 = "MSNBC"
ch234 = "HBO"

What I need is, when I press 2 and 3 on the keyboard, a divs inner html is updated with the matching variable value, "MSNBC"

Any thoughts on the most effective way to do this, anybody?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Duy PhamFreelance IT ConsultantCommented:
I used to create a timeout with half a minute duration when the first key is pressed (2 in your example).
   - If user doesn't press any more key until the timeout elapsed, then I will look for channel 2 (ch2 = "CBS")
   - If user continues press another key (3 in your example), I will joined 2 with 3, and reset the timeout to wait for half a minute more. And if timeout elapsed, then I will look for channel 23 (ch23 = "MSNBC")
dvointeractiveAuthor Commented:
thanks Duy, but that doesn't really answer my question...

Rainer JeschorCommented:
here we go:
// Global channel object
var channels = {};
channels['ch2'] = "CBS";
channels['ch3'] = "ABC";
channels['ch23'] = "MSNBC";
channels['ch234'] = "HBO";

var keyPressedList = "";
var timeoutVar;

$(document).ready(function () {
    $(document).keypress(function( event ) {
        event = event || window.event;
        // Only numbers
        if ((event.keyCode > 47 && event.keyCode < 58) || (event.keyCode > 95 && event.keyCode < 106))
            if (keyPressedList == "")
                keyPressedList += "ch";
            keyPressedList += String.fromCharCode(event.keyCode);
            timeoutVar = setTimeout(function(){ SetChannel();},2000);

function SetChannel() {
    if (channels[keyPressedList]) {
        $("#target").html("No such channel");
    keyPressedList = "";

Open in new window

Live sample:

The timeout is currently 2 seconds (=2000). When you enter a new number value before the timeout occurs, the time is reset and the value appended.


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
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

dvointeractiveAuthor Commented:
Hi Rainer,

MUCH thanks for this, it seems exactly what I was looking for!

The jsfiddle doesn't seem to work for me though...! Not sure why...

Rainer JeschorCommented:
one side note: when you run the jsfiddle, then please click into the redult window / frame and then start typing.
Jsfiddle is based on iframes and the keypress is bound to the iframe document, not the complete (parent) window.
dvointeractiveAuthor Commented:
Yeah, was doing that...

Seems like its not working in my firefox, works in safari!

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

From novice to tech pro — start learning today.