This is a weird issue: I have a HTML attribute that will only be true or false, i fetch it with Jquery and turn it into a boolean, then i switch it for the opposite (true to false, false to true), then i change the same HTML attribute with the new boolean value.
It works at first, when the original attr value is false and it get set to true, but not the other way around:
<button aria-checked="false" class="toggle-search" type="button">
Toggle button is <span>false</span>
</button>
And now the Js/Jquery
$(".toggle-search").click(function(){
var status = !$(this).attr("aria-checked");
// returns string, turn to bool
$(this).attr("aria-checked",!status);
$(this).children("span").html(!status);
});
https://jsfiddle.net/kaqyhc48/4/
I dont understand what's going on with the logic, is the new value not being parsed to false or maybe being parsed to false first and then to true?
Thanks for the help
!"false" === false"true"and"false"you can read them intotrueandfalsewithJSON.parse.aria-checked="false"will be evaluated as true since it is retrieved as a string. You could use the strict equalityif ($(this).attr('aria-checked') === 'false')aria-checkedis a real thing: developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/…