Inside the cshtml view I have a dropdown list with 2 possible options. If selection returns "True", a container with 2 datepickers becomes visible, so user can select a daterange. Whenever I switch dropdown option, both datepickers are assigned with respective values.

$("#ExpirationChoice").change(function () {
    var val = $(this).val();  //picks up either True or False

    if (val) {
        if (val === "True") {
            $("#expirationDates").show();    //show container with datepickers
            $("#BeginDate").val(getFormattedDate(today()));
            $("#EndDate").val(getFormattedDate(today().addMonths(6)));
        }
        else {
            $("#expirationDates").hide();
            $("#BeginDate").val("");
            $("#EndDate").val(getFormattedDate(today()));
        }
    }
    else {
        alert("Please select an option of past or future expiration report.");
    }
});

Let's assume I picked True from dropdown, selected a 1 month range between 12/23/16 and 01/23/17, clicked the button to generate a table.

Then, I switched dropdown option to False, which caused to hide container and reassign date values to empty and today in datepickers (hidden, thus, can't be controlled by user).

Now, when I switch back to True again, my selections are gone, it again assigns it to a 6-month range (I understand why it does it, I will explain my attempts later).

My question is: How to keep my latest selections from True option whenever I switch?

A challenge through my attempts

I understand exactly why it was behaving the following way. My way of thinking is to store selections of datepickers from True option into variables. However, here is a challenge that I am suddenly running into:

if (val === "True") {
   $("#expirationDates").show();

   $("#BeginDate").val(getFormattedDate(today()));
   $("#EndDate").val(getFormattedDate(today().addMonths(6)));

   var start = $("#BeginDate").val();
   var end = $("#EndDate").val();
}

I need to initially assign dates to be a 6-month daterange. So far variables store dates properly. However, whenever I switch, date values will be reassigned back to 6 month range, and it turns into an infinite loop.

Pretty much, I would appreciate any direction in terms of the better approach for such problem.

Related posts

Recent Viewed