Javascript Bug: Set Audio Object Control Attribute True = = False
I'm new to this and don't know exactly how to report a bug, but I want to confirm it first and then continue. But here's what I find:
1) When creating an audio object control attribute, the controls attribute will respond to the string as if it were boolean. For an instance:
<button onclick="example()">Try this</button>
<script>
function example() {
var aud = document.createElement("AUDIO");
aud.setAttribute("src","example.mp3");
aud.setAttribute("controls", "controls");
}
Okay, we put the controls in there because it makes the controls equal: The point is, you can put any old line in there, and it works just fine - apple, banana, pear, whatever.
2) Doesn't that mean the value is boolean? Well, when you try to use boolean false, for example, you get the truth anyway. (False == True) It works as if you had entered true.
... and if you put anything other than true or false (just put anything other than an integer, string, or a true or false value), you get false (or it just doesn't work). Still true, not a string, non-integer is false (or just doesn't work).
Finally, you can even try adjusting the controls attribute on the available audio element:
var aud = document.getElementById("idhere");
function accessAudioElement() {
aud.controls = false;
}
At least here true and false really work as true and false, but again any string or integer will also return you true and any non-string / non-integer number will break the code.
Can someone help me here, because I don't think it should work this way ... and if so, what's the point of using boolean when most others will work?
Of course I am still involved, so maybe it is not a bug, maybe for some reason it should work this way, but if this is the case, someone please share the logic behind this with me.
I just do not understand. Thanks Magic
source to share
You might want to read / learn more about Javascript Truthy $ Falsey. It is very important.
https://j11y.io/javascript/truthy-falsey/
source to share
This is an extended answer to what @nnnnnn suggested in the comments.
aud.controls = false; doesn't set an attribute, it sets a property.
You need to use setAttribute () to add the specified attribute to the element.
aud.setAttribute("controls", "controls");
And use the removeAttribute () method to remove the specified attribute from the element.
aud.removeAttribute("controls");
To learn more about these methods, see the attached hyperlinks.
source to share