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?
dvointeractiveAsked:
Who is Participating?
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")
0
dvointeractiveAuthor Commented:
thanks Duy, but that doesn't really answer my question...

:/
0
Rainer JeschorCommented:
Hi,
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);
            clearTimeout(timeoutVar);
            timeoutVar = setTimeout(function(){ SetChannel();},2000);
        }
    });        
});

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

Open in new window

Live sample:
http://jsfiddle.net/EE_RainerJ/19co0g61/

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.

HTH
Rainer
0

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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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...

:(
0
Rainer JeschorCommented:
Hi,
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.
HTH
Rainer
0
dvointeractiveAuthor Commented:
Yeah, was doing that...

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

thx!
0
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
jQuery

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.