What is the difference between readonly="true" & readonly="readonly"?
Solution 1:
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"
.
Solution 2:
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.
Solution 3:
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.
Solution 4:
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.