tlaverdure
tlaverdure

Reputation: 522

How do you disable all keys with jQuery except backspace?

I can successfully stop all keys from being pressed with:

$this.keydown(false);

How can I disable all keys with the exception of the backspace key?

Upvotes: 4

Views: 4127

Answers (2)

Ry-
Ry-

Reputation: 224906

Check the keyCode of the event argument:

$this.keydown(function(e) {
    if(e.keyCode !== 8) {
        e.preventDefault();
    }
});

Here's a demo.

Upvotes: 13

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

Use a callback, check the keycode of the pressed key, if it's the backspace key then allow it, otherwise block.

Upvotes: 1

Related Questions