It does not appear the jQuery keyup, keydown, or keypress methods are fired when the backspace key is pressed. How would I trap the pressing of the backspace key?
-
Just to clarify, I want to trap when the backspace key is pressed and take an action. I am changing the enabled state of some buttons on the page when the last character is removed from a textbox. Therefore, I still want the backspace key to perform its normal operation. – ChrisP Feb 08 '11 at 17:17
-
1That's not true. It appears in keyup and keydown methods. Only doesn't appear in keypress. – Line Nov 28 '14 at 12:50
-
@RickSmith I don't agree. The question you're referring to was in JavaScript, while the OP here is asking in jQuery. There could be some reason to why someone would want the answer in one method or another. – Racil Hilan Sep 04 '15 at 21:29
-
@RacilHilan You are right, this should not be a duplicate. – Rick Smith Sep 08 '15 at 16:26
4 Answers
try this one :
$('html').keyup(function(e){if(e.keyCode == 8)alert('backspace trapped')})

- 6,562
- 3
- 25
- 33
-
21
-
It works but you cannot in such way show modal window and stop going back to previous page. – Marek Bar Jul 31 '13 at 10:16
-
1
-
Full code example preventing going to previous page as well as using jQuery keyCode constants [below](http://stackoverflow.com/a/32402158/616644). – Rick Smith Sep 04 '15 at 16:00
Regular javascript can be used to trap the backspace key. You can use the event.keyCode method. The keycode is 8, so the code would look something like this:
if (event.keyCode == 8) {
// Do stuff...
}
If you want to check for both the [delete] (46) as well as the [backspace] (8) keys, use the following:
if (event.keyCode == 8 || event.keyCode == 46) {
// Do stuff...
}

- 22,803
- 16
- 52
- 80
Working on the same idea as above , but generalizing a bit . Since the backspace should work fine on the input elements , but should not work if the focus is a paragraph or something , since it is there where the page tends to go back to the previous page in history .
$('html').on('keydown' , function(event) {
if(! $(event.target).is('input')) {
console.log(event.which);
//event.preventDefault();
if(event.which == 8) {
// alert('backspace pressed');
return false;
}
}
});
returning false => both event.preventDefault and event.stopPropagation are in effect .

- 953
- 1
- 11
- 17
The default behaviour for backspace on most browsers is to go back the the previous page. If you do not want this behaviour you need to make sure the call preventDefault()
. However as the OP alluded to, if you always call it preventDefault()
you will also make it impossible to delete things in text fields. The code below has a solution adapted from this answer.
Also, rather than using hard coded keyCode values (some values change depending on your browser, although I haven't found that to be true for Backspace or Delete), jQuery has keyCode constants already defined. This makes your code more readable and takes care of any keyCode inconsistencies for you.
// Bind keydown event to this function. Replace document with jQuery selector
// to only bind to that element.
$(document).keydown(function(e){
// Use jquery's constants rather than an unintuitive magic number.
// $.ui.keyCode.DELETE is also available. <- See how constants are better than '46'?
if (e.keyCode == $.ui.keyCode.BACKSPACE) {
// Filters out events coming from any of the following tags so Backspace
// will work when typing text, but not take the page back otherwise.
var rx = /INPUT|SELECT|TEXTAREA/i;
if(!rx.test(e.target.tagName) || e.target.disabled || e.target.readOnly ){
e.preventDefault();
}
// Add your code here.
}
});

- 1
- 1

- 9,031
- 15
- 81
- 85
-
This handles most cases but I want to prevent back on Select and on certain input types like radio or checkbox, so I removed `SELECT` and added a test on `e.target.type`. So far I have these input types `var rxt = /radio|button|checkbox|image|reset|submit/i;` Color might also be one to exclude but that might be browser specific. – peater Dec 11 '15 at 14:32
-
Update: Chrome disabled this feature and moved the previous page shortcut to ALT + Left Arrow. Can't test other browsers now. – Edgar Froes May 27 '19 at 17:08