1

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 ==.

Ayush
  • 41,754
  • 51
  • 164
  • 239

1 Answers1

10

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.

"true" === true // false
Community
  • 1
  • 1
Matt Stone
  • 3,705
  • 4
  • 23
  • 40