jqueryjquery-uijquery-ui-datepicker

Any way to make lastVal work for a jQuery UI datepicker that is displayed inline?


As I mentioned above, I'm displaying a jQuery datepicker inline and can't get lastVal working. For further illustration I've edited a jQuery datepicker with working lastVal to displaying inline and didn't change anything else and it doesn't work. lastVal is always undefined.

My current jQuery code looks like this:

$("#date1").datepicker({
    changeMonth: true,
    changeYear: true,
    onSelect: function(dateText,inst) {
        if(dateText !== inst.lastVal){
            if (inst.lastVal == '') {
                alert("User has selected: "+dateText);
            } else {
                alert("User has change "+inst.lastVal+" to "+dateText)
            }
        }
    }
});

And my HTML code is just: <div id="date1"></div>


Solution

  • An alternative solution, set your lastVal by yourself:

    $("#date1").datepicker({
    
        changeMonth: true,
        changeYear: true,
        onSelect: function(dateText,inst) {
        
            const myLastVal = inst.myLastVal || "";
            inst.myLastVal = dateText;
            
            if(dateText !== myLastVal){
                if (myLastVal == "") {
                    alert("User has selected: " + dateText);
                } else {
                    alert("User has change " + myLastVal + " to " + dateText)
                }
            }
        }
    });