I need to do something when a user push "." and something else when an user push ":"
Is there a way to intercept these 2 key with javascript, jQuery or other ?
I need to do something when a user push "." and something else when an user push ":"
Is there a way to intercept these 2 key with javascript, jQuery or other ?
Assuming you want to intercept these keys on the whole document:
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = (typeof evt.which == "undefined") ? evt.keyCode : evt.which;
if (charCode) {
var charStr = String.fromCharCode(charCode);
if (charStr == ":") {
alert("Colon!");
} else if (charStr == ".") {
alert("Full stop!");
}
}
};
Marcel Korpel rightly points out in the comments that it's more efficient not to use the String.fromCharCode()
call, so here's a version without:
document.onkeypress = function(evt) {
evt = evt || window.event;
var charCode = (typeof evt.which == "undefined") ? evt.keyCode : evt.which;
if (charCode) {
if (charCode == 58) {
alert("Colon!");
} else if (charCode == 46) {
alert("Full stop!");
}
}
};