'Jquery DatePicker calendar to remembering previous selected date

I have 2 date pickers on my page that are formatted to display month and year. When a user selects the month and year, the page loads data based on the selection. All this works fine. The users are complaining that when they go to click on the calendar again, the options are defaulting to the current month and year and not what the user previously selected. I have tried several option out there regarding trying to set the default date and none seem to work.

Here is what the page looks like on initial load.

enter image description here

After a month is selected and the user has click "Done", the page loads the data. When the user goes back in to the date picker, it is displaying the current month and not the previous selected month.

Here is should be displaying Feb as the month in the calendar selector.

enter image description here

 //set the date picker default dates
        var startDate = new Date();
        var date = new Date();
        var endDate = new Date(date.setMonth(date.getMonth() + 1));


        //set the top navigation
        $("#InventoryFromDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy',
            yearRange: "-5: +00",
            defaultDate: startDate,
            yearRange: "-5: +00",
            onClose: function (selectedDate, inst) {
                $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
                msophysicalinventory.loaddata(selectedDate, toDate);
            }
        });

        $("#InventoryToDate").datepicker({
            changeMonth: true,
            changeYear: true,
            showButtonPanel: true,
            dateFormat: 'MM yy',
            yearRange: "-5: +00",
            defaultDate: endDate,
            onClose: function (selectedDate, inst) {
                $(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
                var fromDate = $("#InventoryFromDate").val();
                msophysicalinventory.loaddata(fromDate, selectedDate);
            }
        });

How do I get the calendar option to show the last selected date if changed?



Solution 1:[1]

There is one way that you need to store somewhere previously selected date and that need to set when the user goes back for date picker.

//Set DatePicker to May 05, 2022

$('#InventoryToDate').datepicker("setDate", new Date(2022,5,05) );

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RAHUL BHOITE