Jquery datepicker onChangeMonthYear

I am using jquery ui datepicker in my application. I created a built-in datepicker. I have a problem with onChangeMonthYear . I have simplified the example to a minimum.

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

  • Accordingly, go to the previous / next month.
  • Set the first day of this month as the current selected date.
  • date warning

The problem is # 2.

I use setDate for this and it ends with infinite recursion. Because I am calling setDate inside onChangeMonthYear. And setDate also runs onChangeMonthYear internally.

How can I achieve these three things when the prev / next button is pressed.

+6
jquery-ui jquery-ui-datepicker datepicker uidatepicker
source share
3 answers

Try setting a flag that you can request in the event handler to prevent an endless loop:

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

Endless recursion means you report the wrong month.
Make sure you reduce 1 from month

+1
source share
 onChangeMonthYear: function(year, month, inst){ $(this).datepicker( "setDate", new Date(year, month-1, 1) ); } 
0
source share

All Articles