72

What is the difference between:

<input name="TextBox1" type="text" id="TextBox1" readonly="true" />

and:

<input name="TextBox1" type="text" id="TextBox1" readonly="readonly" />

When i set readonly to true it works somewhat different from readonly='readonly'. W3C standard says readonly should be 'readonly' & not 'true'. Why most of the browsers allow readonly='true' which has somewhat different functionality than readonly='readonly'?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Ulhas Tuscano
  • 5,502
  • 14
  • 58
  • 89
  • What markup DOCTYPE, DTD, and what browser mode? – Keith May 30 '11 at 06:27
  • 3
    A more global answer for your specific question is that browsers are made to be compatible with all sorts of loose (and sometimes terrible) coding. Figuring out the strictest coding that will work with all browsers is a virtue, because kludges will only work until browser developers get fed up with them :) – Merlyn Morgan-Graham May 30 '11 at 06:38
  • related http://stackoverflow.com/questions/7730695/whats-the-difference-between-disabled-disabled-and-readonly-readonly-for-ht – Adriano Jan 24 '14 at 14:07

6 Answers6

73

Giving an element the attribute readonly will give that element the readonly status. It doesn't matter what value you put after it or if you put any value after it, it will still see it as readonly. Putting readonly="false" won't work.

Suggested is to use the W3C standard, which is readonly="readonly".

Jez
  • 27,951
  • 32
  • 136
  • 233
jerone
  • 16,206
  • 4
  • 39
  • 57
  • 16
    Putting `readonly="false"` won't work in the sense that it won't do the expected thing. It will "work" (have an effect) in the sense that it will make the element readonly (in current browsers, even if it's invalid according to the spec). – ShreevatsaR Oct 31 '16 at 21:07
  • Just to add to that in case it's not clear, to set it to read/write again remove the `readonly` attribute completely – joedotnot Nov 22 '19 at 06:43
59

This is a property setting rather than a valued attribute

These property settings are values per see and don't need any assignments to them. When they are present, an element has this boolean property set to true, when they're absent they're false.

<input type="text" readonly />

It's actually browsers that are liberal toward value assignment to them. If you assign any value to them it will simply get ignored. Browsers will only see the presence of a particular property and ignore the value you're trying to assign to them.

This is of course good, because some frameworks don't have the ability to add such properties without providing their value along with them. Asp.net MVC Html helpers are one of them. jQuery used to be the same until version 1.6 where they added the concept of properties.

There are of course some implications that are related to XHTML as well, because attributes in XML need values in order to be well formed. But that's a different story. Hence browsers have to ignore value assignments.

Anyway. Never mind the value you're assigning to them as long as the name is correctly spelled so it will be detected by browsers. But for readability and maintainability it's better to assign meaningful values to them like:

readonly="true" <-- arguably best human readable
readonly="readonly"

as opposed to

readonly="johndoe"
readonly="01/01/2000"

that may confuse future developers maintaining your code and may interfere with future specification that may define more strict rules to such property settings.

Stonecrusher
  • 184
  • 1
  • 2
  • 14
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
39

readonly="true" is invalid HTML5, readonly="readonly" is valid.

HTML5 spec:

http://www.w3.org/TR/html5/forms.html#attr-input-readonly :

The readonly attribute is a boolean attribute

http://www.w3.org/TR/html5/infrastructure.html#boolean-attributes :

The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false value.

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace.

Conclusion:

The following are valid, equivalent and true:

<input type="text" readonly />
<input type="text" readonly="" />
<input type="text" readonly="readonly" />
<input type="text" readonly="ReAdOnLy" />

The following are invalid:

<input type="text" readonly="0" />
<input type="text" readonly="1" />
<input type="text" readonly="false" />
<input type="text" readonly="true" />

The absence of the attribute is the only valid syntax for false:

<input type="text"/>

Recommendation

If you care about writing valid XHTML, use readonly="readonly", since <input readonly> is invalid and other alternatives are less readable. Else, just use <input readonly> as it is shorter.

Community
  • 1
  • 1
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • 2
    +1, but note that the same counts for HTML 4.01 according to [the spec](http://www.w3.org/TR/html401/intro/sgmltut.html#h-3.3.4.2). – user247702 Aug 28 '14 at 12:40
20

readonly="readonly" is xhtml syntax. In xhtml boolean attributes are written this way. In xhtml 'attribute minimization' (<input type="checkbox" checked>) isn't allowed, so this is the valid way to include boolean attributes in xhtml. See this page for more.information.

If your document type is xhtml transitional or strict and you want to validate it, use readonly="readonly otherwise readonly is sufficient.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
2

I'm not sure how they're functionally different. My current batch of OS X browsers don't show any difference.

I would assume they are all functionally the same due to legacy HTML attribute handling. Back in the day, any flag (Boolean) attribute need only be present, sans value, eg

<input readonly>
<option selected>

When XHTML came along, this syntax wasn't valid and values were required. Whilst the W3 specified using the attribute name as the value, I'm guessing most browser vendors decided to simply check for attribute existence.

Phil
  • 157,677
  • 23
  • 242
  • 245
  • This has nothing to do with XML being valid, but rather **well formed**. Validity is related to correct naming and valuing of attributes, their nesting etc. Validity has to do with **schema**. – Robert Koritnik May 30 '11 at 06:44
  • @Robert I'm referring to XML **syntax** though. Valid XML attribute syntax is `attribute_name="value"` – Phil May 30 '11 at 06:54
  • @Phil: No that's a **well formed** attribute. A valid attribute is a `int_attribute="1"` or `bool_attribute="false"`... They're of course well formed as well. – Robert Koritnik May 30 '11 at 06:58
  • @Robert I'm not talking about validating documents. Perhaps I should use the word "legal" in place of "valid" or any other word that defines the accepted or correct use of characters (ie, syntax) – Phil May 30 '11 at 07:06
  • @Phil. Well formed XML document is one that passes XML formatting validation (ie. single root element, closing element tags **and** attributes have values (any) assigned to them). But valid XML document is one that is valid against some schema (ie. certain attributes must have correct value type assigned to them, or certain elements can only be nested in particular elements etc.) In your case you're talking about well formed XML attributes (so they have some value assigned to them). – Robert Koritnik May 30 '11 at 07:13
  • @Robert Forgetting XML for a minute, would you agree that a piece of computer program code (or markup) may be **syntactically** valid or invalid. If so, then understand that is the definition of **valid** I'm using – Phil May 30 '11 at 07:18
  • @Phil. I know what you mean. I'm just trying to tell you that this *validness* has a well known name. So why call it any other than *well formed XML*. But never mind. We both know the point of this. And I don't want to argue against otherwise completely fine answer. – Robert Koritnik May 30 '11 at 07:51
1

According to HTML standards, the use of

<input name="TextBox1" type="text" id="TextBox1" readonly/>

is enough to make the input element readonly. But, XHTML standard says that the usage listed above is invalid due to attribute minimization. You can refer to the links below:

https://www.w3.org/TR/xhtml1/diffs.html#h-4.5

http://www.w3schools.com/tags/att_input_readonly.asp

James Jithin
  • 10,183
  • 5
  • 36
  • 51