0

I read out a data-attibute, then I want to convert the string "true" into a boolean. At the moment I have to do a comparison in the javascript, is there a better way to do it? I don't know how to use this solution

HTML

<div data-nav='{ "nav": "true"}'>

JS

    var data = JSON.parse($nav.attr('data-nav').toString());
    data.nav = (data.nav === "true") ? true : false; 
Community
  • 1
  • 1
vuvu
  • 4,886
  • 12
  • 50
  • 73

1 Answers1

-2

Try

<div id='test' data-nav='true'>


var truefalse = $('#test').data('nav');

.data should be able to evaluate it as boolean

here is an example in JSFiddle https://jsfiddle.net/lismore/hx3gLvgw/

Lismore
  • 259
  • 2
  • 11