Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML - Why boolean attributes do not have boolean value?

Tags:

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!

like image 758
dpp Avatar asked Aug 17 '11 08:08

dpp


People also ask

Do boolean attributes always need a value?

If the attribute is present, its value must either be the empty string or a value that is an ASCII case-insensitive match for the attribute's canonical name, with no leading or trailing whitespace. The values "true" and "false" are not allowed on boolean attributes.

Does HTML have Boolean?

Post from May 19, 2020 (↻ May 25, 2022), filed under Web Development. There are Boolean attributes in HTML and, unless I err somewhere, there are two dozen of them. Here's a quick rundown of what makes for a Boolean attribute, and what Boolean attributes there are in current HTML.

How do you add a Boolean value in HTML?

To add a Boolean attribute: node. setAttribute(attributeName, ''); // example: document. body.

Which of the following attributes is not a Boolean attribute?

Correct Option: C Muted is Boolean attribute whose value is set as false by default. Buffered is not boolean attribute in <audio> element.


2 Answers

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.

like image 131
Chuck Avatar answered Oct 13 '22 20:10

Chuck


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.

like image 22
Álvaro González Avatar answered Oct 13 '22 20:10

Álvaro González