23

I noticed that some elements have attributes which are boolean. I wonder why the values are not true or false? or 1 and 0? Are there any reason behind why they are like this?

<option selected="selected">Ham Burger</option>

or

<input type="button" disabled="disabled" />

Thanks in advance!

kazinix
  • 28,987
  • 33
  • 107
  • 157
  • Try `selected="true"`, `selected="1"`, `selected`. As long as `selected` is there, the option will be selected. – Shef Aug 17 '11 at 08:11
  • 5
    And if I put false? It will still selected :) That would be confusing, right? he he. – kazinix Aug 17 '11 at 08:15
  • 1
    Yes, it might be confusing, but the short-form is suggested, with [implied boolean true when the attribute is present, false otherwise](http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2). – Shef Aug 17 '11 at 08:18
  • 6
    As a side note - I'm glad to see that there are still people out there that question things we got used to, just because we were told so ;-) – sgibly Aug 17 '11 at 08:24
  • I'm late to the party but the [WHATWG HTML5 spec](http://www.whatwg.org/specs/web-apps/current-work/#boolean-attributes) recommends the short-form and _specifically proscribes_ giving the attribute any value except for the empty string or the name of the attribute. – chucksmash Apr 18 '13 at 20:21
  • 1
    @chucksmash and it's a good thing they did; that way you don't have weirdos using something like `selected="false"` to mark an element as selected. – Doktor J Jul 25 '14 at 18:04

7 Answers7

17

In SGML, an attribute may be minimized so that its value alone is short for both the name and the value, with the only possible value for the attribute in this case obviously being the attribute's own name. HTML uses this for boolean attributes, where the presence or absence of the attribute is what's meaningful, and its value is irrelevant. But in XML, minimized attributes were disallowed, so we wound up with the awkwardness that is selected="selected" when XHTML came into vogue. If you're writing HTML rather than XHTML, you can just write selected.

Chuck
  • 234,037
  • 30
  • 302
  • 389
  • 1
    "...SGML ... its name alone is short for both the name and the value". Are you sure? My understanding is that the SGML definition is such that the value implies the name, not vice versa. – Alohci Aug 17 '11 at 08:34
  • 2
    This answer is complete I think. `presence or absence of the attribute is what's meaningful` and `XML, minimized attributes were disallowed, so we wound up with the awkwardness that is selected="selected"` – kazinix Aug 17 '11 at 08:53
  • @Alohci: Good catch — I believe I did say it backwards. Thanks for the correction. – Chuck Aug 17 '11 at 15:53
  • One more reason why HTML is a joke. – Neme Aug 29 '17 at 17:28
8

The exact definition is:

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). 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".

Also:

Boolean attributes may legally take a single value: the name of the attribute itself [...] In HTML, boolean attributes may appear in minimized form

Basically, this implies that there are only two possible statuses for boolean attributes, true and false, but there isn't a not set status.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
3

For the disabled attribute I think it's the presence of the attribute that disables the element regardless of its value.

It guess one of the reasons could be to allow more values than just yes/no in the future. For instance, instead of visible=true/false, you can have visibility=visible/hidden/collapsed

vc 74
  • 37,131
  • 7
  • 73
  • 89
1

the HTML standard (Not the XHTML) is to have simply selected instead of selected="selected"

See here: http://www.w3.org/TR/html4/interact/forms.html#adef-selected

When XHTML was created to allow a a better integration with XML in HTML, (see http://www.w3.org/MarkUp/2004/xhtml-faq#need), the parts that do not fit to the XML-like structure requirements of HTML were corrected. So wordings like selected got transformed into selected="selected" to fit the standard

uncovery
  • 794
  • 5
  • 19
0

Readability, a lot of HTML is not coded by people with computer science backgrounds so the concept of "Boolean" would be foreign to them in those terms. Also it improves readability for Computer Science and other technical users by providing reinforced clues as to the function of a given statement.

0

As vc74 has said, it doesn't matter what value you have for selected or disabled.

<option selected="selected">Ham Burger</option>

will do the same as

<option selected="sjkhdaskj">Ham Burger</option>

Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99
0

i think this is just for ease to user to specify the attribute value in most human readable form if he/she dont know what is true/false

<html>
<body>
<select>
<option>1</option>
<option selected="blah">2</option>
<option >3</option>
</select>
</body>

you see in above code i have not use selected=selected, i used what i want it still select the option value, or you can simply use <option selected>2<option>.

Aamir Rind
  • 38,793
  • 23
  • 126
  • 164
  • `selected="blah"` is not valid in any dialect of HTML or XHTML. Pretty much any browser will be able to suss out what you mean, but it's still wrong. – Chuck Aug 17 '11 at 08:30
  • For me as a human being it's kinda redundant actually to say "a dog's brown is brown" (or is wrong actually). I would prefer "a dog is color brown". :) – kazinix Aug 17 '11 at 08:32
  • yes i know its not a good way, what i want to say that it accept every value for attribute "selected" – Aamir Rind Aug 17 '11 at 08:39