How to get the value of each key pressed and use it in a variable with jQuery?

How do I get the value of each key pressed and use it in a variable with jQuery? I want to press a key and show a specific picture on a page that correlates with that key when it is pressed. I also ONLY want to target AZ and "."

Thank!

+5
source share
3 answers

Using jQuery, you can use the event keypressand then convert the character to a string and match it with your criteria.

Here's a working example :

$(document).keypress(function(e)
{
    var s = String.fromCharCode(e.which);
    if (s.match(/[a-zA-Z\.]/))
        console.log(s + ' is a match!');
});

:. $('#LearnStart'), .

+7

heres jquery Keypress

0

ids, #. id LearnStar script :

$("#LearnStar").live("keypress",function(e)
{
    var s = String.fromCharCode(e.which);
    if (s.match(/[a-zA-Z\.]/))
        console.log(s + ' is a match!');
});

, script.

, , , e.preventDefault() false .

0

All Articles