I want to catch the event with javascript or jquery when user presses to the delete button in keyboard. Is this possible?
Asked
Active
Viewed 129 times
1
-
thanks,can you give me an example? – Afsun Khammadli May 29 '13 at 11:23
-
for example if delete pressed alert a message I am fresh for jquery – Afsun Khammadli May 29 '13 at 11:25
-
I solve this problem sucs as ` $('html').keyup(function(e) { if (e.keyCode === 46) alert('Delete Key Pressed'); });` – Afsun Khammadli May 29 '13 at 11:30
2 Answers
4
I'm not sure why you're being told that this is impossible?
The delete key is 46, so this should work:
$('html').keyup(function(e){
if(e.keyCode == 46){
//delete key has been pressed.
}
})
Also note: You should be using either the keyup or keydown event rather than keypress. keypress is intended for real character. 'keyup'/'keydown' is handled at a lower level so it will capture all non-printing keys like DEL and ENTER.
Source is here: Capturing "Delete" Keypress with jQuery

Community
- 1
- 1

johnkavanagh
- 4,614
- 2
- 25
- 37
-
-
'Semicolons in JavaScript are optional': http://mislav.uniqpath.com/2010/05/semicolons/ Take a read! – johnkavanagh May 29 '13 at 11:49
-
Thanks @RokoC.Buljan I'm aware of the limitations with minification. In this instance it would make no difference. For reference: many JS compilers/minifiers will strip unnecessary semi-colons, including the YUI Compressor. – johnkavanagh May 29 '13 at 11:52
-
Glad to hear it @ƏfsunXəmmədli - feel free to open up more questions in the future if you need further guidance from people here. If your question has been answered, do remember to put a green tick against the relevant answer so that anyone coming to the same problem in the future can find the answer as easily as possible. – johnkavanagh May 30 '13 at 08:24
0
$('html').keyup(function(e){
if(e.keyCode == 46)alert('Delete Key Pressed')
})
from here

Community
- 1
- 1

Bhavin Rana
- 1,554
- 4
- 20
- 40
-
jQuery will handle `which` like all other charcode keycode guys. – Roko C. Buljan May 29 '13 at 11:47