4

I've been reading some articles about HTML, XHTML, etc. In most of them (i.e. My preferred syntax style) say that boolean attributes should be written without any value, like this:

<input type="text" required>

They even say that it is wrong to use this attributes like this:

<input type="text" required="required">

Some of this articles link W3 which says:

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.

but in the examples shows like this:

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>

So, how should the boolean attributes be written? Based in your experience, which of the options are cross-browser and which are not?

Diego
  • 16,436
  • 26
  • 84
  • 136
  • 1
    Why the "but" in "but in the examples shows like this"? – bzlm Sep 07 '11 at 14:58
  • "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" it doesn't say can be valueless or something like that – Diego Sep 07 '11 at 15:00
  • No value resolves as "the empty string". – bzlm Sep 07 '11 at 15:03
  • Is `readonly=""` the same as `readonly` for all browsers? – Diego Sep 07 '11 at 15:04
  • Well, that's your question. I hope somebody actually answers it. :) I'm just wondering why you considered the W3 spec contradictory. – bzlm Sep 07 '11 at 15:05
  • @Diego I'm pretty sure that all browsers understand valueless boolean attributes (like ``). – Šime Vidas Sep 07 '11 at 15:09

2 Answers2

7

Attribues without values are valid in HTML, but invalid in XHTML, because it's not allowed in XML. Perhaps that's where your confusion is coming from. So, which one is valid depends on the doctype of your document.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Are value-less attributes like `disabled` (as opposed to `disabled="disabled"`) allowed in HTML4 as well as HTML5? – mindplay.dk Feb 25 '15 at 17:30
2

I always use checked="checked" and disabled="disabled". I don't really have a reason for adding it, but it has always worked in all browsers that I test in. This includes IE6+.

Seth
  • 6,240
  • 3
  • 28
  • 44