json1 json1 - 1 year ago 63
jQuery Question

Boolean("false") returns true.. any alternative?

I am using jquery to write true and false strings to data- html attributes. However when I write false to one of the attributes, and then check it it false, it returns true. I also read online that it supposed to do that, which really makes no sense at all.

Does anyone have an alternative? Any help would be really appreciated!

(function() {
// $(".opt-header").click(function () {
$(document).on("click", ".opt-header", function() {
var is_activate = Boolean($(this).attr("data-is-panel-activated"));
if (is_activate) {
$(this).find(".fa-angle-right").replaceWith("<i class='fa fa-angle-down'></i>");
$(this).attr("data-is-panel-activated", "false");
} else {
$(this).attr("data-is-panel-activated", "true");
$(this).find(".fa-angle-down").replaceWith("<i class='fa fa-angle-right'></i>");

Answer Source

Boolean('false') will return true because the string 'false' is truthy.

You could just run a check to see if the string equals 'false':

if ($(this).attr("data-is-panel-activated") === 'false') {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download