views:

965

answers:

1

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.

+3  A: 

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;
    }
});
Simon Lieschke