← Back to HTML & CSS, page 8

html-css-0158 · Easy fresher

Why is disabled="false" still disabled, and what are the rules for quoting attribute values?

A boolean attribute is on when present, whatever its value, so disabled="false" is still disabled. Remove the attribute or set the property to false. Quote values that contain spaces or special characters, and note that the first duplicate attribute wins.

Full answer

A boolean attribute is on when it is present and off when it is absent. The value does not matter. <input disabled>, <input disabled=""> and <input disabled="false"> are all disabled. The only way to turn it off is to remove the attribute. This is a common bug when a template writes disabled="{{ flag }}" and the flag is false.

In JavaScript, use the property: input.disabled = false removes the attribute, and input.disabled = true adds it. The same holds for checked, required, hidden, readonly, open, selected, multiple, autofocus and controls. Note that checked as an attribute sets the default state, while the property is the live state after the user clicks.

Quoting rules:

  • A value needs quotes when it has spaces, quotes, an equals sign, or angle brackets. class=a b gives the class a and a separate attribute named b.
  • Quotes can be double or single. Use the other kind inside the value, or write &quot;.
  • Unquoted values are legal when they are simple, but a quoted style is safer and easier to read.
  • Attribute names and element names are not case sensitive in HTML. Values can be, for example type="text" is not case sensitive, but id and class values are.
  • If an attribute appears twice, the first one is used and the later ones are ignored.

Some attributes look boolean but are not. contenteditable, draggable, spellcheck and aria-hidden take the values true or false, and aria-hidden="false" is meaningful.

The first input is disabled even though the value says false. The div gets the class big and a separate attribute named small. The input with two value attributes shows the first one. The last div shows that draggable and aria-hidden read their values as text.

Use the property in scripts, and use frameworks' boolean bindings and not strings.

html
<input id="a" disabled="false">
<input id="b" required>
<div id="c" class=big small></div>
<input id="d" value="first" value="second">
<div id="e" title='He said "hi"'></div>
<div id="f" draggable="false" aria-hidden="false"></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Boolean attributes depend on presence, not value.
  • Use the property in scripts: el.disabled = false.
  • Quote values with spaces or special characters.
  • The first duplicate attribute wins.
  • draggable and aria-hidden take true or false.

Common mistakes

  • Writing disabled="false" to enable a control.
  • Leaving out quotes around a class value with a space.
  • Treating aria-hidden or draggable as boolean attributes.

Follow-ups

  • How do you turn a boolean attribute off in a script?
  • What happens when an attribute is written twice?

Tips

  • State the rule: present means on, absent means off.
  • Mention the property versus attribute difference for checked.