Compare jquery .data() with String always false

bluelDe

First of all the fiddle: http://jsfiddle.net/6d82Q/1/

In my HTML I have:

<div class="myClass" data-status="true"></div>
<div class="myClass" data-status="false"></div>

I want to add a CSS-class to a div depending on the status:

$('.myClass').each(function()
{ 
    // compare status
    if($(this).data('status') == 'true')
    {
        $(this).addClass('myTrueClass');
    }
    else
    {
        $(this).addClass('myFalseClass');
    }
});

The Problem is, that always myFalseClass is added. My question is: How can the problem be fixed and why does it occur in the first place?

zzzzBov

The .data() method will perform some auto-casting on [data-*] attributes, which makes passing data to the client very convenient.

As you're storing what would be auto-cast to a boolean, you can use:

if ($(this).data('status')) {
    ...
} else {
    ...
}

If you need to make sure that the value is true, and not just truthy (like a value of 1) then be sure to use a reference comparison:

if ($(this).data('status') === true) {
    ...
} else {
    ...
}

As an aside, there's no reason to call .each() when you could simply pass a function to .addClass():

$('.myClass').addClass(function () {
    return $(this).data('status') ? 'myTrueClass' : 'myFalseClass';
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

jQuery compare object always resulting false

Bcrypt compare always returning false

Why can't i compare this two string ? The string input is the same, but it always return false

when i enter a String by input from user and compare it with other string , it always false, explained below ,WHY?

always return false when compare an existing string from a test file to the newly enterer object with parameter

bcrypt.compare with Promise always return false

Compare password in WordPress always return false

Python - Compare number with boolean (always return false)

Java - comparing string always false

Spring Data Comparison always false

compare string with data frame

bcrypt-nodejs compare function always return false

Cypress-compare file contents with an array always return false

jquery attribute selection always returning false?

jQuery validation addmethod always return false

jQuery validator addMethod always returning false

Checkbox state always false - HTML, JQuery

Checking if string is null or undefined always returns False

In javascript, is an empty string always false as a boolean?

JSTL string comparison always returns false

Why string comparision results always false

String.contains always appears false

Comparison of two String return always false java

String comparison from nested for returns always false

Part of string comparison always inexplicably returns False

Why always return "None" string, like my if statment was always false?

JQuery compare string using if not working

Wrong result string compare jquery

jQuery compare string to input value