Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you disable all keys with jQuery except backspace?

Tags:

jquery

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?

like image 705
tlaverdure Avatar asked Dec 08 '22 18:12

tlaverdure


2 Answers

Check the keyCode of the event argument:

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

Here's a demo.

like image 86
Ry- Avatar answered Dec 11 '22 10:12

Ry-


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

like image 28
Niet the Dark Absol Avatar answered Dec 11 '22 10:12

Niet the Dark Absol