How to make a html control be read only?

M

Matt

How to make a html control (text box, checkbox, etc...) be read only?
For example, in the following text box code, value "Joe" is in text
box, but user still able to edit it. I don't want user able to edit
it.

<input type="TEXT" name="username" value="Joe">

Please advise!
 
P

PW

Matt said:
How to make a html control (text box, checkbox, etc...) be read only?


Try using "disabled" . (see example below)

<input type=text name="Total" DISABLED value="" size="10" maxlength="10">

You can also try "readonly".

HTH,
PW
 
T

Toby A Inkster

PW said:
Try using "disabled" . (see example below)
<input type=text name="Total" DISABLED value="" size="10" maxlength="10">
You can also try "readonly".

That is one possibility, but it begs the question of whether <input> is
really an appropriate element to use in the first place.

If Matt doesn't want the end user to be able to input a new value, why use
<input> at all. Why not (for example) use:

<span style="border: 1px solid black; padding:2px">Joe</span>
 
J

Jukka K. Korpela

Toby A Inkster said:
That is one possibility, but it begs the question of whether
<input> is really an appropriate element to use in the first place.

Indeed. And I wouldn't advice anyone to give advice on such things
unless the advisor _knows_ the difference beteen disabled and readonly
and doesn't need to ask others to "try" it.
If Matt doesn't want the end user to be able to input a new value,
why use <input> at all. Why not (for example) use:

<span style="border: 1px solid black; padding:2px">Joe</span>

That would show (when CSS is enabled) the word "Joe" in a box, which
would make it look a bit like an input field. I don't think that would
be adequate in most cases, since it would suggest that the user _can_
change it and it would mean that the string "Joe" is not passed to the
form handler (which might not be needed actually, but I doubt that).

***

The question was multiposted, so apparently the OP is not interested in
getting quickly the best possible answer. For others, here's a copy of
my response in alt.html.tags:

How to make a html control (text box, checkbox, etc...) be read only?

Using the attribute readonly (or if you play the XHTML game,
readonly="readonly") in an <input> element. But by the specification,
it applies to elements with type="text" and type="password" only
(though browsers might be more liberal).
For example, in the following text box code, value "Joe" is in text
box, but user still able to edit it. I don't want user able to edit
it.

<input type="TEXT" name="username" value="Joe">

So why do you have the field there in the first place?

It's safer to use

<input type="hidden" name="username" value="Joe">
Username: Joe

partly because not quite all browsers support the readonly attribute,
partly because this makes it much clearer that the user is not expected
to change the username.
 
L

Leif K-Brooks

Matt said:
How to make a html control (text box, checkbox, etc...) be read only?
For example, in the following text box code, value "Joe" is in text
box, but user still able to edit it. I don't want user able to edit
it.

Why do you want to do that? Input boxes are for _input_, not display.
Why not put "Joe" on the page as normal text?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top