Philipp Braun
Philipp Braun

Reputation: 1573

How can I disable all keyboard keys?

I am searching for a possibility to disable all keyboard keys in a textarea. I found some examples on the web to disable some single ones but how can I disable the whole keyboard??

Upvotes: 8

Views: 44613

Answers (4)

Robert Kajic
Robert Kajic

Reputation: 9077

You can disable the textarea using the disabled attribute. Or you can make it read only using the readonly attribute. readonly is like disabled except it doesn't prevent the user from clicking or selecting in the textarea.

And if you really need to do it with javascript something like this will do it (using jQuery):

<textarea rows=3 cols=20></textarea>​

$("textarea").keydown(false);

You can try it here: http://jsfiddle.net/7n4G4/1/

Upvotes: 19

Tobias Krogh
Tobias Krogh

Reputation: 3932

use the readonly attribute if you want to make sure that your user can still click and select inside of the textarea

<textarea name="text" readonly>

if you want to keep it completely disabled without selecting

<textarea name="text" disabled>

if want to prevent only keyboard input

<textarea name="text" id="text-input">

very simple with jQuery

$("#text-input").on("keydown keypress keyup", false);

Upvotes: 5

Manishearth
Manishearth

Reputation: 16188

 document.getElementById('ID OF TEXTAREA').onkeypress=function(){return false;}

Dunno why you want to do that if you can just disabled it.

Upvotes: 0

ZER0
ZER0

Reputation: 25322

Assuming you have in textare the reference of your textarea, maybe something like:

textearea.onkeydown = textarea.onkeypress = function() { return false };

That it's probably an overkill, but... Considering to use the attribute readonly instead:

textarea.readOnly = true

In the first case you're still able to copy/paste using the mouse, in the second case you can only copy (using keys or mouse doesn't matter).

Upvotes: 3

Related Questions