JQuery
JQuery

Reputation: 907

Jquery boolean not working in if()

I'm probably missing something obvious and have trued this in many ways but can't seem to get this working;

I have a

@Html.HiddenFor(m => m.AddEdit.IsStartValue, new { id = "IsStartValueHidden" })

for a bool value in my model.

in my script on the page I have this

var start = new Boolean();
                    start = $('#IsStartValueHidden').val();
                    console.log(start);
                    if (start == true) {
                        console.log("if start true");
                        $('#StartValue').attr("disabled", "disabled");
                    }
                    else {
                        console.log("If start false");
                    }

the first console.log writes a true value, however it doesn't hit the if() statement but goes to the else and writes false!

I have written it in many ways such as if($('#IsStartValueHidden').val() == true..

but however I have done it, it always logs the value as true but seems to see it as false in the if section and skip to the else.

Really confused how to do this!

I've tried using If(.. === true) also)

Upvotes: 3

Views: 3536

Answers (2)

CaptainHere
CaptainHere

Reputation: 698

var start = new Boolean();
                    start = $('#IsStartValueHidden').val();
                    console.log(start);
                    if (start =="true") {
                        console.log("if start true");
                        $('#StartValue').attr("disabled", "disabled");
                    }
                    else {
                        console.log("If start false");
                    }

Upvotes: 1

pouyan
pouyan

Reputation: 3439

not sure but i think this would work (because i think your $('#IsStartValueHidden').val() return string value):

                var start = $('#IsStartValueHidden').val() == "true";
                console.log(start);
                if (start == true) {
                    console.log("if start true");
                    $('#StartValue').attr("disabled", "disabled");
                }
                else {
                    console.log("If start false");
                }

Upvotes: 5

Related Questions