I want to focus an input field in the document's KeyDown event if it's not already focused. In case that it is already focused, nothing is supposed to happen. If it's not focused yet, the text content is to be deleted and the pressed key – if its char code is in a specific range – to be added.
My current code only works in the newest browsers, not even in Firefox 3.0 for example ...
$(document).keydown(function (e) {
var code = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
if (code < 33 || code > 256) {
return;
}
// Check if the text input is focused
if ($("*:focus").attr("id") == "searchString") {
// Maybe more to come here ...
}
else {
// Clear content and focus
$("#searchString")
.val("")
.focus();
}
});
e.charCodeore.keyCode, try usinge.whichwhich is normalized in jQuery