Html – Retrieve boolean data from data attribute in jquery


I am trying to retrieve a data attribute of type Boolean from an html DIV element , however it is always returning false when the string is converted to boolean .


<div id='test' data-return="true"></div>


isreturn_str = $('#test').data('return');
isreturn = (isreturn_str === 'true');
if (isreturn) {
    document.write("It is true");
} else {
    document.write("It is false");


It is false

Best Solution

The jQuery .data() method is smart about recognizing boolean and numeric values and converts them into their native type. So this returns the boolean true, not "true":


If you want to get the raw data (without the automatic data conversion), you can use .attr():


See the working test case here: