I am trying to toggle the data-isAdmin to true and back in a toggle loop, here is my code:
<div id="is-admin" data-isAdmin="false" onclick="toggleIsAdmin();">
NOT Admin
</div>
<script>
toggleIsAdmin() {
let item = document.getElementById('is-admin').getAttribute('data-isAdmin');
item2 = Boolean(item);
alert(item2); // ==> true
alert(!item2); // ==> false
}
</script>
It should return false and after the flip return true.
I need the string "false" to be understood by the Boolean() function as false and not true. It is literally spelled f a l s e and not t r u e.
If Boolean() is not able to understand that and will interpret any string, even an empty one like "" as true, then what other function can understand the string "false" as false and the string "true" as true?
Or is there a simpler way to toggle it?
I was expecting Boolean() function to understand "false" to mean false.