Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

in jquery ui datepicker, on the OnSelect() event is there anyway to get the previous selected date

i have code on the onSelect event of the jquery ui datepicker and i now want to only run my function if the date has changed values (so if a user selects a date that was already there in the textbox, i don't want to run this code as it will be a redundant calculation). Here is my existing code.

$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: calcUpdate
});
like image 915
leora Avatar asked Apr 22 '11 01:04

leora


2 Answers

According to http://api.jqueryui.com/datepicker/#option-onSelect, you should try this:

$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: function(curDate, instance){
        if( curDate != instance.lastVal ){
            //so, the date is changed;
            //Do your works here...
        }
    }
});

The onSelect function receives 2 parameters which are used here; You can debug/console the values of the 2nd parameter to know more about it.

like image 169
Reza Mamun Avatar answered Nov 15 '22 07:11

Reza Mamun


You can use data to store previously stored value and compare the current value to it.

Try this(put these statements in your document ready event):

$('#Milestone').data("prev", $(this).val());
$('#Milestone').datepicker({
    dateFormat: 'dd M yy',
    onSelect: function(dateText){
            var prevDate = $(this).data("prev")
            var curDate = dateText;
            if(prevDate == curDate){
              $(this).data("prev", curDate)
                calcUpdate();
            }
        }
});
like image 42
Chandu Avatar answered Nov 15 '22 07:11

Chandu