PDA

View Full Version : making a textbox uneditable


martinjsmith
08-06-2002, 12:39 PM
Hi there,

Anyone know how to make a standard HTML textbox uneditable i.e. the text in it cannot be deleted or amended?

Martin

Zvona
08-06-2002, 01:18 PM
Standard :
<input type="text" readonly="readonly" onfocus="this.blur();" />

If you want to disable(gray) text-box :
<input type="text" disabled="disabled" />

beetle
08-06-2002, 07:23 PM
Zvona, I've never seen it done that way. Interesting.
You should be able to just add the readonly attribute

<input type="text" readonly>

As far as I know, this is standard.

boxer_1
08-06-2002, 07:32 PM
Originally posted by beetle
Zvona, I've never seen it done that way. Interesting.
You should be able to just add the readonly attribute

<input type="text" readonly>

As far as I know, this is standard.

Some older browser versions don't support the standard way of disabling a form field. onFocus="this.blur();" pretty much takes care of that by removing focus from the form field as soon as it is applied, preventing the value from being changed ;).

beetle
08-06-2002, 07:46 PM
I meant the readonly="readonly" part....I'm pretty (or was) sure that readonly is boolean, and shouldn't need to receive a string value like that.

I'm very familiar with the blur() method :D

boxer_1
08-06-2002, 07:58 PM
Originally posted by beetle
I meant the readonly="readonly" part....I'm pretty (or was) sure that readonly is boolean, and shouldn't need to receive a string value like that.

I'm very familiar with the blur() method :D

Oh, I thought you were talking about the onFocus="this.blur();"...sorry. Yes, the readonly can simply be given a true or false value :).

http://www.mozilla.org/xpfe/xulref/textbox.html

beetle
08-06-2002, 08:02 PM
So isn't it technically invalid to use readonly="readonly"?? That's what I'm trying to get at....

Roy Sinclair
08-06-2002, 09:10 PM
Originally posted by beetle
So isn't it technically invalid to use readonly="readonly"?? That's what I'm trying to get at....

As of xHTML 1.0 <input... readonly="readonly"> is technically valid and <input... readonly> isn't valid though browsers do accept it.

See http://www.w3.org/TR/xhtml1/#h-4.5

beetle
08-06-2002, 09:16 PM
Ya, but what if you aren't using an XHTML DOCTYPE, or no DOCTYPE at all? (which, correct me if I'm wrong, most people still do unless they are concerned with XHTML)

BTW...thanks for the link.

Roy Sinclair
08-06-2002, 10:15 PM
Originally posted by beetle
Ya, but what if you aren't using an XHTML DOCTYPE, or no DOCTYPE at all? (which, correct me if I'm wrong, most people still do unless they are concerned with XHTML)

BTW...thanks for the link.

readonly="readonly" works in any browser which accepts the readonly property in the first place (at least every browser I know).

boxer_1
08-06-2002, 10:32 PM
http://www.w3.org/TR/2001/WD-xforms-20010608/index-all.html See 5.2.3 readOnly (use Ctrl F to find quickly). Not exceptionally useful, but worth a mention ;).

adios
08-08-2002, 03:58 AM
readonly="readonly"
selected="selected"
disabled="disabled"

http://www.webreview.com/tag/2000/09_01_00.shtml

Also:

<input....onfocus="if(this.readOnly==null)this.blur()">

...unless you want it to run everywhere. It generally produces a bit of a flicker, although it works well enough.

Zvona
08-08-2002, 10:29 AM
These aren't valid :
readonly="true"
disabled="false"

These are valid in HTML DTD:
readonly
disabled

Above is called attribute minimalization, which isn't allowed in XHTML nor XML declarations. Thus, they've replaced them with :
readonly="readonly"
disabled="disabled"

However, you can alter attributes through scripting :
document.frmName.elemName.readonly = true;
document.frmName.elemName.disabled = false;

beetle
08-08-2002, 03:05 PM
Thanks for all the great info people!