42

It seems to be common consensus that for XHTML attributes which do not require any value, we should repeat the attribute name. E.g. <input disabled> in correct XHTML is <input disabled="disabled"/>.

However, we can get the HTML <input> element to be disabled using any of the following:

  • <input disabled=" "/>

  • <input disabled=""/>

  • <input disabled="asdfg">

  • <input disabled="false">

Is there actually an official rule to use disabled="disabled"? Or is it a matter of taste?

NorvayHais
  • 601
  • 1
  • 7
  • 10
  • i can't find *any* version of XHTML that says this use is required. so if you could find even one version that says so, it basically answers the question. – NorvayHais Jun 30 '11 at 08:11
  • @NTan - I have supplied an answer with a link to the W3C standards. You may also want to refer to validator.w3.org to validate your mark-up against the official standards. – Fenton Jun 30 '11 at 08:16
  • 2
    `!` stand for not in most languages I know. `disabled="not"` looks confusing to me. – Dorus Jun 30 '11 at 08:19
  • 2
    @Dorus so I think you will prefer `disabled="yes"` or `disabled="true"` compared to `disabled="disabled"` – NorvayHais Jun 30 '11 at 08:23

4 Answers4

51

The officially correct xhtml syntax is disabled="disabled".

The reason for this is that xhtml is an XML syntax, and XML requires that attributes have values. The xhtml specs also explicitly specify that the value should be "disabled".

The reason for the choice of this value over any other possible value was fairly arbitrary; they simply decided that all previously boolean attributes should be converted to XML format by making their value the same as their name.

So yes, there is an official spec which says you must use that full syntax. But it only applies to xhtml documents. You can find it here (if you search for disabled in that page, you will find that it is listed as only allowing "disabled" as the value. Similarly for the readonly and checked attributes).

Plain HTML - both v4 and v5 - isn't tied to XML's restrictions in this way, and doesn't require an attribute value for disabled; the mere existence of the disabled attribute is sufficient to disable the field, regardless of whether you have a value for the attribute, or what that value is.

The final upshot of all this is that if you are using an XHTML doctype, or you wish to remain XML-compliant, you should use disabled="disabled". If you're not using XHTML and you don't care about having valid XML syntax, then you can just use disabled on its own, or with any attribute value you like.

One other thing I would note (getting slightly off topic, but may be relevant) is that this may have an impact on any CSS or JQuery code that may reference the field. For example, I've seen people using JQuery selectors such as $('[disabled=disabled]'), and similar in CSS. This obviously relies on the attribute having the expected value. Therefore, if you're going to reference a boolean attribute like this in a selector, you should reference it without a value, like so: $('[disabled]') as this will work whatever the attribute is set to.

Hannele
  • 9,301
  • 6
  • 48
  • 68
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • 4
    attribtues in xhtml should be double-quoted like this disabled="disabled". I know a lot of browsers don't mind using single quotes, but it won't pass as xml or xhtml without – Per Hornshøj-Schierbeck Jun 30 '11 at 08:12
  • 1
    this doesn't answer the question. I know that we need a value to make it adhere to the XML syntax. But all the examples that i've provided in the question are XML-valid syntax (they all have an attribute surrounded by quotes). Now the question is is there any specs that says the value must be 'disabled' ? as i've demonstrated in the question, `disabled="hjkl" works fine in all browsers since 20 years ago. – NorvayHais Jun 30 '11 at 08:14
  • 4
    @Per Hornshøj-Schierbeck - by my reading of the [XML spec](http://www.w3.org/TR/REC-xml/#NT-AttValue), specifically `AttValue`, either single or double quotes may be used to surround an attribute value. – Damien_The_Unbeliever Jun 30 '11 at 08:21
  • @Per - I agree with @Damien; I understood it to allow either single or double quotes. However, I've changed the answer to use double quotes, since it makes no difference to the answer and I was editing it anyway. – Spudley Jun 30 '11 at 08:24
  • 2
    @N.Tan - I've updated the answer to expand on the reasons for the attribute being that way, and also to link to the W3C's DTD documentation which specifies it. Hope that helps answer the question a bit better. – Spudley Jun 30 '11 at 08:25
  • 1
    @Spudley don't even use `[disabled]`, there's one selector for this reason: [:disabled](http://api.jquery.com/disabled-selector/). – TWiStErRob Jul 19 '13 at 09:46
  • 1
    @TWiStErRob - yes indeed, `:disabled` exists for exactly this reason. However that won't work in pure CSS in older browsers. (which are still relevant, and were even more so two years ago when the question was asked) – Spudley Jul 19 '13 at 09:50
6

There is documentation for this, the official term for these kind of attributes is "Boolean Attributes"

The official standard is on the W3C website http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

It says that:

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

For your XHTML mark-up to be valid, you must use the long-hand

<option selected="selected">

For HTML, you can use the minimised form

<option selected>
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • 1
    sry although i've tagged the question as HTML (because i'd thought that xhtml is in fact related to html), the question explicitly states XHTML yet the link is a HTML rule. – NorvayHais Jun 30 '11 at 08:31
  • 3
    Okay folks... please do a little reading on the history of XHTML1! – Fenton Jun 30 '11 at 10:58
4

@Spudley has already given you the link to the XHTML specs to provide the official documentation.

The choice of making both the attribute and the value the same in XHTML was not entirely arbitrary though.

As @Sohnee says, XHTML 1.0 was a reformulation of HTML4.01 in XML and the goal was to stick to HTML 4 patterns as much as possible to make the transition as easy as possible for web authors.

HTML has always supported disabled="disabled" as a valid form for boolean attributes, and the reason for that is it permitted the shortened attribute disabled to be defined in SGML. (Note @Thaddee Tyl's "bit odd" comment)

In SGML, the disabled on it's own attribute isn't the attribute name without a value, it's the attribute value without a name. i.e. the name is inferred from the value. To make all this work in SGML and be backward compatible with what browsers have always done, the name and the value have to be defined to be the same.

Note that this only affects SGML based validation. Browsers' parser logic isn't SGML based and has never cared for this subtlety, hence you can, in practice put in whatever value for the attribute you like.

HTML5 validation isn't SGML based either, so the restriction has been relaxed. disabled="" is now valid. disabled="true" and disabled="false" are not valid though, because disabled="false" is confusing, since as you note, it disables, not enables the control. See http://www.w3.org/TR/html5/common-microsyntaxes.html#boolean-attributes for details.

Alohci
  • 78,296
  • 16
  • 112
  • 156
1

An html4 document specifies this:

Boolean attributes may legally take a single value: the name of the attribute itself.

Boolean attributes were always a bit odd in the sgml world, so really you can put anything you want. Browser implementation is all that matters.

Thaddee Tyl
  • 1,126
  • 1
  • 12
  • 17
  • 1
    sry although i've tagged the question as HTML (because i'd thought that xhtml is in fact related to html), the question explicitly states XHTML – NorvayHais Jun 30 '11 at 08:30