I have an anchor element :
<a id="buyBtn" href="#" alt="Purchase" title="Buy now" data-enabled="true">
When it is clicked, I’m doing this:
enabled = $(this).data('enabled') == "true";
console.log(enabled);
However, the console shows false. I was initially using === but that was giving false, so I moved to ==.
The accepted answer for this question details why:
Retrieve boolean data from data attribute in jquery
jQuery’s
.data()method is smart enough to convert “true”/”false” data strings into real Boolean values.The strict comparison operator checks types, and is failing because you are comparing a string to a boolean.