3

What is the proper way (standards compliant) way to add selected, disabled and similar attributes to <input> elements in HTML?

I have seen:

<input type="text" disabled>

<input type="text" disabled="disabled">

<input type="text" disabled="yes">

As far as I can tell, they all work, regardless of what the attribute's value is.
What is the right way to do this?

Aillyn
  • 23,354
  • 24
  • 59
  • 84

1 Answers1

4

disabled is a boolean attribute.

disabled="disabled" is the correct form; disabled alone is shorthand allowed in HTML.

From On SGML and HTML:

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

In HTML, boolean attributes may appear in minimized form -- the attribute's value appears alone in the element's start tag. Thus, selected may be set by writing:

<OPTION selected>

instead of:

<OPTION selected="selected"> 

Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form.

Community
  • 1
  • 1
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • It seems like regardless of what I use, even "falsy" values like `disabled=""`, `disabled=false`, `disabled=0` the result is still the same – Aillyn Oct 08 '10 at 21:50
  • Also: "Authors should be aware that many user agents only recognize the minimized form of boolean attributes and not the full form." So it seems like a recommendation to use `disabled` rather than `disabled="disabled"`? – Aillyn Oct 08 '10 at 21:52
  • @pessi yes, true. Although I know of no browser that can't deal with `disabled="disabled"`. – Pekka Oct 08 '10 at 21:57
  • @pessi regarding the falsy values, `disabled=""` *should* be false in my understanding but it's possible that introducing the attribute at all already makes it true. – Pekka Oct 08 '10 at 21:58
  • @pessi what I said in my last comment seems to be true. From the link: `Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".` – Pekka Oct 08 '10 at 21:59