55

what is the code to disable an INPUT text box for HTML?

Thanks

5 Answers 5

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

See the W3C HTML Specification on the input tag for more information.

Sign up to request clarification or add additional context in comments.

Comments

58
<input type="text" required="true" value="" readonly="true">

This will make a text box in readonly mode, might be helpful in generating passwords and datepickers.

2 Comments

Surely adding the 'required="true"' part to a readonly textbox in general is a bad idea. There could well be cases where that textbox doesn't get a value put into it. Then your form won't be submitted and there will be little the user can do about it.
This answer helped me start to discover the difference between disabled & readonly; readonly being what I ultimately needed to prevent user interaction but still submit the prior-entered value. See htmlcodetutorial.com/forms/_INPUT_DISABLED.html
10

The syntax to disable an HTML input is as follows:

<input type="text" id="input_id" DISABLED />

3 Comments

RoBorg's answer is valid in XHTML also.
The HTML 4.1 Specification he directs to, has the same code as in my answer.
HTML 4.1 is outdated. XHTML has to be valid, so browsers display it more consistently from one to the other, as different browsers have different error-handling code for cleaning up after sloppy developers (and might not get it right).
10

You can Use both disabled or readonly attribute of input . Using disable attribute will omit that value at form submit, so if you want that values at submit event make them readonly instead of disable.

<input type="text" readonly> 

or

 <input type="text" disabled>

2 Comments

I'm not sure this answers the posters question, and id probably better suited as a comment to one of the other answers, or re-written to include the new point of the disable attribute
@stuart-siegler: I guess it does. However I've added the example.
2
<input type="text" required="true" value="" readonly>

Not the.

<input type="text" required="true" value="" readonly="true">

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.