Link to home
Start Free TrialLog in
Avatar of dvointeractive
dvointeractiveFlag for United States of America

asked on

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?
Avatar of Duy Pham
Duy Pham
Flag of Viet Nam image

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")
Avatar of dvointeractive

ASKER

thanks Duy, but that doesn't really answer my question...

:/
ASKER CERTIFIED SOLUTION
Avatar of Rainer Jeschor
Rainer Jeschor
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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...

:(
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
Yeah, was doing that...

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

thx!