Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between readonly="true" & readonly="readonly"?

Tags:

html

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'?

like image 248
Ulhas Tuscano Avatar asked May 30 '11 06:05

Ulhas Tuscano


People also ask

What is readonly true?

readonly="true" is invalid HTML5, readonly="readonly" is valid. 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.

What is difference between readonly and disabled?

The difference between disabled and readonly is that read-only controls can still function and are still focusable, whereas disabled controls can not receive focus and are not submitted with the form and generally do not function as controls until they are enabled.

What is readonly readonly?

Definition and Usage The readonly attribute is a boolean attribute. When present, it specifies that an input field or textarea is read-only. A read-only field cannot be modified (however, a user can tab to it, highlight it, and copy the text from it).

What does readonly attribute do?

Read-only is a file attribute which only allows a user to view a file, restricting any writing to the file. Setting a file to “read-only” will still allow that file to be opened and read; however, changes such as deletions, overwrites, edits or name changes cannot be made.


2 Answers

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".

like image 102
jerone Avatar answered Oct 01 '22 03:10

jerone


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.

like image 39
Robert Koritnik Avatar answered Oct 01 '22 01:10

Robert Koritnik