Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery boolean not working in if()

Tags:

jquery

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)

like image 846
JQuery Avatar asked Jan 18 '26 03:01

JQuery


2 Answers

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");
                }
like image 126
pouyan Avatar answered Jan 20 '26 21:01

pouyan


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");
                    }
like image 28
CaptainHere Avatar answered Jan 20 '26 20:01

CaptainHere



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!