3

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)

JQuery
  • 889
  • 3
  • 13
  • 35

2 Answers2

5

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");
                }
pouyan
  • 3,445
  • 4
  • 26
  • 44
  • Hi, yes i just clicked on that, noticed it was logging True and not true. – JQuery Mar 04 '17 at 14:44
  • So how do you get the bool value to store in a variable as a bool? – JQuery Mar 07 '17 at 19:36
  • i didn't understand you, but in 'var start = $('#IsStartValueHidden').val() == "true";' i store the boolean into a variable. – pouyan Mar 07 '17 at 20:05
  • say $('#IsStartValueHidden') is the id of a @Html.checkBoxFor() how do you store the bool value, true OR false? var x = $('#IsStartValueHidden').val() still store a string not bool? – JQuery Mar 07 '17 at 20:11
  • if your input type is 'checkbox' you can use 'var x = $('#IsStartValueHidden').checked()' instead. – pouyan Mar 07 '17 at 20:15
  • thanks but thats not a function? "Uncaught TypeError: $(...).checked is not a function" – JQuery Mar 07 '17 at 20:53
  • this link would help you : http://stackoverflow.com/questions/2204250/check-if-checkbox-is-checked-with-jquery – pouyan Mar 08 '17 at 06:50
1
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");
                    }
CaptainHere
  • 698
  • 6
  • 14