Randomblue
Randomblue

Reputation: 116353

"freezing" an input box

I have an <input type="text" /> element. At some point, I don't want it to accept text anymore. For some reason, changing the type to type="button" for example is not allowed.

How can I make my <input> element not accept text anymore?

Upvotes: 2

Views: 8172

Answers (5)

Connor O&#39;Hara
Connor O&#39;Hara

Reputation: 139

With JavaScript, you can do document.getElementById("textbox").disabled = "disabled";, which will make it read only.

Upvotes: 0

Lea Hayes
Lea Hayes

Reputation: 64216

Just disable the control with:

<input type="text" disabled="disabled" />

But be warned, it is still possible for a hacker to manipulate submitted form data. You should ensure that appropriate server-side validation is applied.

If using jQuery you can do the following:

HTML:
<input id="my-text-box" type="text" />

JS - jQuery 1.5-:
$('#my-text-box').attr('disabled', 'disabled');
JS - jQuery 1.6+:
$('#my-text-box').prop('disabled', true);

Upvotes: 1

Kon
Kon

Reputation: 27441

Make it read-only:

$('#myInput').attr('readonly', 'readonly');

Upvotes: 9

colithium
colithium

Reputation: 10327

You need to modify the readonly attribute.

Upvotes: 0

Ariel
Ariel

Reputation: 26773

Add the READONLY attribute (property in jQuery).

Upvotes: 0

Related Questions