0

I am having some trouble with variables in javascript and asp.net mvc project.

I am building an app where the user has the option to display a page in full-screen format.

When the user wants to display a page in full screen format and click the submit key they should get a warning before the page is submitted.

I am having some problems with the javascript that handles the condition if the user should get a warning or not however. The following code illustrates the problem:

Javascript:

    <script type="text/javascript">

    var $full = false; //initiates the full variable

    $(document).ready(function () {
        $("#TemplateID").change(function () { //when selected index of dropdown is changed
            var selectedIndex = $("select option:selected").first().text(); //the selected index
            $.get('@Url.Action("getIfFullscreen", "Messages")', //calls an ASP.net method to determine if selected index should be displayed as fullscreen
                { id: selectedIndex }, function (data) {
                    $full = data; //sets the variable full to return value
                    Debug.writeln("onchange:" + $full) //displays as expected
                });
        });
    });

    $(document).ready(function () {
        $('[data-confirm]').click(function (e) {
            Debug.writeln("onclick:" + $full); //displays as expected
            if ($full == true){ //always takes the value from when the variable full was initiated
                if (!confirm($(this).attr("data-confirm"))) {
                    e.preventDefault();
                }
            }
        });
    });
</script>

HTML:

<div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" data-confirm="Are you sure?" value="Skicka" class="btn btn-default" />
                    </div>
                </div>

It seems like the variable full is changed (in the first function) from the look of the debug output but the if statement always interprets it as it was first initiated.

I am quite new to javascript and any help is greatly appreciated.

3
  • 1
    What is the value you see in $full ? Are you setting it to boolean true or a string "true" ? Commented Mar 27, 2016 at 16:15
  • I am setting it with boolean true/false from the asp.net part. I think you have pointed me in the right direction. I changed the full - variable to use integers instead and now it seems to work when i use 0 and 1 instead of true and false. Thank you very much! Commented Mar 27, 2016 at 16:24
  • Yea. I added an answer explaining why it could be failing. Commented Mar 27, 2016 at 16:40

1 Answer 1

1

If your server code is returning a boolean true value, It will come as True, not true. In javascript True is not boolean true. So your js variable is going to be overwritten with the string value "True"

So you may change your if condition to check against the string value

if ($full === "True"){  }
Sign up to request clarification or add additional context in comments.

1 Comment

Works perfectly! Thank you very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.