jquery datepicker onChangeMonthYear
-
11-09-2019 - |
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: -
- Go to the previous/next month accordingly.
- Set the first day of that month as the current selected date.
- 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.
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