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?
The removeAttribute() method removes an attribute, and does not have a return value. The removeAttributeNode() method removes an Attr object, and returns the removed object.
Use the removeAttribute() to remove an attribute from a specified element. Setting the value of a Boolean attribute to false will not work; use the removeAttribute() method instead.
To set an attribute without a value, select the element and call the setAttribute() method on it, e.g. button. setAttribute('disabled', '') . If we pass an empty string as a second parameter to the setAttribute method, the attribute is set without a value.
setAttribute() Sets the value of an attribute on the specified element. If the attribute already exists, the value is updated; otherwise a new attribute is added with the specified name and value. To get the current value of an attribute, use getAttribute() ; to remove an attribute, call removeAttribute() .
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.
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With