tags:

views:

15

answers:

1

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?

+2  A: 

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.

Pekka
It seems like regardless of what I use, even "falsy" values like `disabled=""`, `disabled=false`, `disabled=0` the result is still the same
pessimopoppotamus
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"`?
pessimopoppotamus
@pessi yes, true. Although I know of no browser that can't deal with `disabled="disabled"`.
Pekka
@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
@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