1

I want to know if it is possible to minimize this code by making a function, a loop or any other trick to avoid a block of code 26 lines.

$("#inputText").bind('keyup',function(objEvent){
switch(objEvent.keyCode)
{
case 65: $("div:contains('A')").attr("class", styledClass); break;
case 66: $("div:contains('B')").attr("class", styledClass); break;
...
case 90: $("div:contains('Z')").attr("class", styledClass);
}
})

1 Answer 1

2

Use String.fromCharCode:

$("#inputText").bind('keyup',function(objEvent){
    var letter = String.fromCharCode(objEvent.keyCode);
    $("div:contains('" + letter + "')").attr("class", styledClass);
})
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome! It works, thanks. Also I forgot to put "styledClass" between quotes.
Great! Please set answer as correct, clicking on the gray V sign near the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.