8

I was reading something about boolean attribute here, which says that for a boolean attribute (in this particular example, loop attribute of <audio>), whatever value you set, it is going to be recognized as "true". In order to really set to falsy, you cannot set it like loop=false or with javascript as ['loop']=false, but have to remove the attribute such as by doing removeAttribute('loop'). Is this true?

I first believed it, but as far as checked it with Chrome, it seems that setting to ['loop']=false will actually do make it be recognized as falsy. I am not sure how robust this fact is when considered cross-browserly. Is there any difference among browsers?

sawa
  • 165,429
  • 45
  • 277
  • 381
  • I prefer the word [falsy](http://en.wikipedia.org/wiki/JavaScript_syntax#Boolean) – mplungjan Jan 08 '12 at 06:15
  • 2
    According to this, having the value present is considered true and you'd be safer going that way rather than setting false. - http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2 – JohnP Jan 08 '12 at 06:16

4 Answers4

6

Boolean attributes are explained here:

http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.3.4.2

Some attributes play the role of boolean variables (e.g., the selected attribute for the OPTION element). Their appearance in the start tag of an element implies that the value of the attribute is "true". Their absence implies a value of "false".

Boolean attributes may legally take a single value: the name of the attribute itself (e.g., selected="selected").

So, while some browsers may interpret the string "false" as if the value was not set, others may not decide to (which is the correct behavior). Actually, as far as I know (or thought), any non-empty string usually sets the value to on/true (regardless of what the spec says is a legal value). I believe this is also undefined behavior, so this may change as well or be different from browser to browser (don't rely on it).

The bottom line is, just because a browser or two may deviate from the spec doesn't mean that you should. Removing the attribute entirely is the way to go.

Addendum: Looking at your comments and question a little closer, I think you may be confused about attribute values in general. In HTML, attr=false and attr="false" are exactly the same. Quotes are not required in any version of HTML (unless they are needed to remove ambiguity when the value contains spaces). For instance:

<input class=required>
<!-- This is fine -->

<input class=title required>
<!-- this is fine too, but "required" will be parsed as an attribute -->

<input class="title required">
<!-- To have two classes, we need the quotes -->

All attribute values (on elements that have them) are treated as strings. In other words, there is no such thing as a true boolean value (or NULL value) in HTML like there is in javascript.

Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • Thanks for the addendum, but I am aware that in html, they are interpreted as strings. That is why I was mentioning javascript. In javascript, I can do things like `['loop']=false` without quotes around `false`, and that is different from `['loop']='false'`. Your answer helped. – sawa Jan 08 '12 at 07:02
  • @sawa: Sorry about the addendum then. I've never seen anything like `['loop']=false` in javascript for that, perhaps `element.loop = false`. Not sure where you got that syntax? I also see you're from CT. So am I. Sorry about that as well :) – Wesley Murch Jan 08 '12 at 07:38
  • I see, you are also in CT. Your answer helped me, and I can't see problem in it. Thanks for the help. – sawa Jan 08 '12 at 08:03
  • @WesleyMurch what about scenario like this: parent defines `contenteditable`, but the child wants to disable it? If we remove the attribute, it "inherits" the property of being editable from the parent. – rr- May 15 '16 at 19:48
  • Okay, never mind: `contenteditable` is not a boolean attribute, but rather an enumerated attribute with possible values of `true`, `false` and `''`. Talk about inconsistency. – rr- May 15 '16 at 19:53
1

Just so anyone who needs this in the future:

loop=false remains true unless the entire loop attribute is removed. Basically, the presence of just loop is what a tag needs to do something else. You need to use something like jQuery to remove the entier loop attribute (or at least that's what I would do). Now, if you set a different undefined attribute to false, then you are able to recognize it as false.

blake305
  • 2,196
  • 3
  • 23
  • 52
1

The audio element is an HTML5 element, so regarding its meaning, you should consult the HTML5 drafts. In this case, see the definition of boolean attributes in the developer version of the WHATWG draft. It says, in effect, a) that the presence or absence of the attribute determines whether the DOM attribute value is true or false, and b) as a requirement on documents, the value must be empty or (case-insensitively) the attribute name, in this case loop='' or loop="loop". The use of quotation marks around the value are defined elsewhere.

Thus, browsers are required to recognize loop=false to mean the same as loop=loop or loop=true, but authors must not use such constructs, and HTML5 checkers issue error messages about them.

(Basically, you’re supposed to use just loop in HTML serialization of HTML5 and loop="loop" in XHTML serialization.)

Thus, if you have a variable x in JavaScript with an audio element object as its value, x.loop has the value true or false whereas x.attributes['loop'].value indicates the value used in HTML markup (which is usually not interesting as such).

There’s a further complication as regards to Firefox: it still does not seem to support the loop attribute (see the question HTML5 Audio Looping). This means that if you set e.g. loop="loop", x.attributes['loop'].value will be loop but Firefox does not even set x.loop (i.e., it is undefined), still less implement the functionality.

Community
  • 1
  • 1
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0

You're confusing strings and real Boolean types. Javascript has a Boolean datatype with two possible values of true and false (without quotes). Strings can contain any text, so can they contain "true" and "false" with quotes. Setting a property to non-null and non-false yields true, so the following will accour:

var a = true; // true
var b = false; // false
var c = "true"; // true
var d = "false" // true
var e = null; // false;
var f = 0; // false
var g = 1; // true

Note the similarities with C.

  • He's actually referring to the HTML part of it and not JS. The question isn't tagged properly – JohnP Jan 08 '12 at 06:22
  • Oh yes, sorry. Misunderstood the question –  Jan 08 '12 at 06:23
  • Hey, it's not the case that I was not reading carefully enough... it's the case that it's tagged "javascript"! –  Jan 08 '12 at 06:24
  • I am doing it through javascript, and am distinguishing `false` and `"false"`. – sawa Jan 08 '12 at 06:25