1

According to W3C spec...

The contenteditable attribute is an enumerated attribute whose keywords are the empty string, true, and false. The empty string and the true keyword map to the true state.

All of the examples and documentation I've found represent the "empty string" as follows:

<div contenteditable="">...</div>

I'm wondering if the following is equally valid (cross-browser) for the sake of cleaner, briefer code:

<div contenteditable>...</div>

It works just fine - at least on current versions of Firefox and Chrome. I'm just wondering if it's valid and reliable cross-browser. My thinking is that it's comparable to form attributes like readonly, disabled, selected, etc - which are commonly and reliably used without assigning a value.

SOLUTION:

Marc B pointed out the following from the W3C spec:

Note that empty attribute syntax is exactly equivalent to specifying the empty string as the value for the attribute.

...which basically confirms the interchangeability of absent and empty values for boolean attributes/properties, meaning both code snippets above are valid and (should be) treated identically across browsers. Thanks to all who answered!

JoshR
  • 461
  • 3
  • 9
  • 1
    I may be wrong, but I think [this part of the spec](http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#boolean-attributes) implies that a value-less attribute represents `true`, as opposed to an attribute with an empty value, which would be the empty string. Based off of [this question](https://stackoverflow.com/questions/9729080/are-empty-html5-data-attributes-valid). – ajp15243 Jan 22 '15 at 18:51
  • Thank you. This is what I suspected, but I can only find references to form attributs/properties being represented this way (without value). No such references to "contenteditable," which is why I'm not 100% confident in its validity/reliability. – JoshR Jan 22 '15 at 19:25

3 Answers3

1

As per the W3C specs:

In the following example, the disabled attribute is given with the empty attribute syntax:

<input disabled>

Note that empty attribute syntax is exactly equivalent to specifying the empty string as the value for the attribute, as in the following example.

<input disabled="">

Now, that's what the W3C says, and then there's what the browser makes read. You can be sure that Internet Explorer will translate/read that as "argle bargle screw the specs woofle" and do its own thing, probably treating "disabled" as "do_the_stupidest_possible_thing_repeatedly=true".

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • 1
    To be fair, IE11 is very standards compliant, just about on par with Chrome and FF. – ajp15243 Jan 22 '15 at 19:01
  • Speaking as someone who's been doing web development since almost the time that the Web was something you telnetted into a computer at CERN to try out, screw IE. It will **ALWAYS** be a piece of garbage, and its sole purpose in the universe is to download any OTHER browser. – Marc B Jan 22 '15 at 19:02
  • Suit yourself, so much for being fair. – ajp15243 Jan 22 '15 at 19:13
  • Thank you. This is the first time I've seen "note that empty attribute syntax is exactly equivalent to specifying the empty string as the value for the attribute," which - if applicable to all boolean attributes - is the confirmation I've been looking for! – JoshR Jan 22 '15 at 19:39
  • 1
    @ajp: there's being fair, and then there's IE. M$ has any number of chances to build a good browser, and rammed IE6 down the planet's throats for far too long. it wasn't until FF and then Chrome started eating their lunch that they got scared and started even MINIMAL improvements in the browser. They had far too many chances, and the only thing they deserve now is a slow death by torture for all the suffering they've given the web. There is no redemption for them. Only revenge. – Marc B Jan 22 '15 at 19:42
  • @MarcB Wow, what a hateful outlook. – ajp15243 Jan 22 '15 at 19:53
  • On the other hand, IE 4 was revolutionary and way ahead of everything at the time. Recent versions of IE have been huge improvements. I wouldn't be surprised if the next Microsoft browser was excellent. – Tim Down Jan 23 '15 at 10:50
1

According to WHATWG leaving out the value should lead to inherit, and not true as the empty string does. Being different values, I would say it is not safe.

But if you want clear code, why not write true, since you need to ask this question you can't even read your own code. And I had to look up both what "" does and the missing value default for the attribute, that is not what I would call clear.

Chewie
  • 71
  • 6
  • Thank you. My goal isn't clear code, otherwise the more explicit the better, right? According to the spec, omitting the ATTRIBUTE is equivalent to "inherit," but including the attribute and setting its VALUE to empty is equivalent to "true". My question is whether an OMITTED value is treated the same as an EMPTY value. Please refer to the code snippets in my question if the difference isn't clear. – JoshR Jan 22 '15 at 19:36
  • Apologies, I can actually not find a specification from WHATWG on how to treat the attribute with no keyword. -- tried to do a new line, sec for quote .. – Chewie Jan 22 '15 at 23:00
  • 1
    The quote I was looking for is what Marc B wrote in his answer, I just have nothing for exactly what attributes it applies to, "Certain attributes may be specified by providing just the attribute name, with no value." Nothing about which. – Chewie Jan 22 '15 at 23:15
  • Actually I think the WHATWG spec you cite implies that when the value is omitted, `true` is implied, because a missing value is equivalent to an empty string, as per the spec (https://html.spec.whatwg.org/multipage/syntax.html#attributes-2). – Tim Down Jan 24 '15 at 11:03
0

Here's a helpful link from Mozilla that should accurately answer your question.

Extract:

According to MDN the contentEditable property when set to true, or an empty string, indicates that the element is editable. Because the contentEditable attribute is enumerated and not Boolean if you leave off the value it is inherited from the parent element.

This is supported back to IE6, Chrome 11, Firefox 3.0 (Gecko 1.9), Opera 10.6, and Safari 3.2.

Tyler
  • 129
  • 9
  • 1
    Link-only answers are discouraged, as they are susceptible to [link rot](https://en.wikipedia.org/wiki/Link_rot). It is best to reproduce the relevant info from the link in your answer. – ajp15243 Jan 22 '15 at 18:58
  • Thank you. I understand that an empty string is equivalent to "true," but what I'm asking is if an ABSENT string is a valid way of representing an empty string. To illustrate, please refer to the code snippets in my question. – JoshR Jan 22 '15 at 19:23
  • I apologize for misreading your original quesiton @JoshR. Simply writing contenteditable within the tag is **Not** allowed. You must explicitly use one of the three. – Tyler Jan 22 '15 at 19:28
  • Thanks @quandary1011101. Marc B's finding in the W3C spec suggests otherwise and was the answer I accepted. Can you reference a contradictory source? – JoshR Jan 22 '15 at 23:04