Setting bool value for input and getting string type using javascript / jquery

When I get a value or set a value (for example, from the server side) for input, which is a boolean type, it comes as "True" or "False" instead of true or false.

For example:

//"True"
var isDated = $('.someClass').val();


//will not pass because isDated is a string
if (isDated == true){
    console.log("passed");
}

Why is this happening? What is the best way to avoid this?


EDIT:

I found a blog that has a solution to avoid this problem: http://www.peterbe.com/plog/data-and-attr-in-jquery

Below is a prototype of the .toBoolean () method for checking true / false when it comes as a string based on some answers from this post:

String.prototype.toBoolean = function ()
{
    var dictionary = { "True": true, "False": false };
    return dictionary[this];
};

Boolean.prototype.toBoolean = function () {
    return this;
};

Number.prototype.toBoolean = function ()
{
    if (this) {
        return true;
    }

    return false;
};
+4
source share
1

, # True False , . .

, , , <input> elements JavaScript. , . /, .checked ( jQuery $('.someClass').is(':checked'), $('.someClass').prop('checked')). , : if ($('.someClass').val().toLowerCase() === 'true').

+5

Source: https://habr.com/ru/post/1607321/


All Articles