Accessing the Delete key in Google Chrome via JavaScript
-
07-07-2019 - |
Question
I use the following code to delete a character when the user presses the delete code. It works in Firefox but doesn't work in Google Chrome. What do I have to fix?
window.onkeypress = function(key) {
...
if (Guessing>0){
else if (key.keyCode == 8){//Delete key
PhraseEntry=PhraseEntry.substring(0, PhraseEntry.length - (1+back))
+ PhraseEntry.substring(PhraseEntry.length - back, PhraseEntry.length );
$("display_text").empty( ).append(PhraseEntry);
}
}
}
...
Solution
Try the keydown
event instead (Delete
is keyCode
46):
window.onkeydown = function(e) {
if (e.keyCode == 46) { // Delete key
// ...
}
}
OTHER TIPS
<script type="text/javascript">
window.onkeypress = function(key)
{
alert(key.keyCode);
}
</script>
I'd start with this - put that in your page and hit Delete when viewing it in Chrome :)
Handle the keydown
event instead.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow