Question

I am using the jquery ui datepicker in my app. I have created an inline datepicker. I am having a problem with onChangeMonthYear. I have simplified the example to bare minimum.

Onclick of "prev" or "next", the calendar should: -

  1. Go to the previous/next month accordingly.
  2. Set the first day of that month as the current selected date.
  3. alert that date

The problem is with #2.

I am using setDate to do that, and it is ending up in infinite recursion. Because, I am calling setDate inside onChangeMonthYear. And setDate is also firing onChangeMonthYear internally.

How can I achieve those 3 things when prev/next is clicked.

Was it helpful?

Solution

Try setting a flag that you can query in your event handler to prevent the infinite loop:

var changingDate = false;
$('.selector').datepicker({
    onChangeMonthYear: function(year, month, inst) {
        if (changingDate) {
            return;
        }
        changingDate = true;
        // your setDate() call here
        changingDate = false;
    }
});

OTHER TIPS

The infinite recursion means you are informing the incorrect month.
Make sure you are decreasing 1 from month

onChangeMonthYear: function(year, month, inst){
    $(this).datepicker( "setDate", new Date(year, month-1, 1) );
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top