2

Any ideas why result of charCodeAt(0) and e.keyCode don't match? and how do I fix this situation?

var stop_symbols = $("#words_stop_symbols span").text().split('').map(function (val) {return val.charCodeAt(0);});
    console.dir(stop_symbols);
    //
    $(document).on("keydown", ".quick-edit", function(e) {
        console.dir(e.keyCode);
        if ($.inArray(e.keyCode,stop_symbols) != -1) {
            // 
            console.dir("blocked char")
            return false;
        }
    });

1 Answers1

1

Using the keypress event will work :

$(document).on("keypress", ".quick-edit", function (e) {
    if ($.inArray(e.keyCode, stop_symbols) != -1) {
        return false;
    }
});

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • @salivan - In my opinion it is, someone else had a different opinion and downvoted my answer, at least until I posted the fiddle as "proof", but this does work just fine as the keypress event and charCodeAt should return the exact same representation of the characters passed (keyup and keydown does not). – adeneo Nov 01 '13 at 23:32
  • I changed my code to this: $.inArray(String.fromCharCode(e.keyCode),stop_symbols But it gave me same incorrect results untill I used keypress. So you have the answer. thanks! –  Nov 01 '13 at 23:39