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?

6
  • 1
    Why the "but" in "but in the examples shows like this"? Commented Sep 7, 2011 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 Commented Sep 7, 2011 at 15:00
  • No value resolves as "the empty string". Commented Sep 7, 2011 at 15:03
  • Is readonly="" the same as readonly for all browsers? Commented Sep 7, 2011 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. Commented Sep 7, 2011 at 15:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Are value-less attributes like disabled (as opposed to disabled="disabled") allowed in HTML4 as well as HTML5?
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+.

3 Comments

Right now I'm in the same position.
Based on other comments I would use them. I know it might not happen often, but if you go to work for a client who uses XHTML strict you won't run into issues.
what about checked='on' ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.