1

while I'm using html attributes, I saw boolean attributes and I have used it many times.

but when I went to mdn docs to boolean attributes I have read that Boolean attributes can only have one value, which is generally the same as the attribute name.

and also as we know boolean attributes can be written without a value. but while I do some experiments I saw weird behave

<input type="text" disabled />

this is an input elemnet which has boolean attribute without value and it works correctly.

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

this is an input elemnet which has boolean attribute that has value of the same name as their name and it also as expected it works correctly.

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

here an input element with boolean attribute which has value of anything and it works correctly.

why boolean attribute that has anything as value works correctly?

2 Answers2

0

Check the HTML standard for any questions of syntax. To quote it:

A number of attributes are boolean attributes. The presence of a boolean attribute on an element represents the true value, and the absence of the attribute represents the false 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. To represent a false value, the attribute has to be omitted altogether.

Here is an example of a checkbox that is checked and disabled. The checked and disabled attributes are the boolean attributes.

<label><input type=checkbox checked name=cheese disabled> Cheese</label>

This could be equivalently written as this:

<label><input type=checkbox checked=checked name=cheese disabled=disabled> Cheese</label>

You can also mix styles; the following is still equivalent:

<label><input type='checkbox' checked name=cheese disabled=""> Cheese</label>

Blindy
  • 65,249
  • 10
  • 91
  • 131
0

why boolean attribute that has anything as value works correctly?

Browsers have vast numbers of error handling routines for recovering from invalid HTML.

That is one of them.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335