Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery datepicker onChangeMonthYear

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.

like image 314
Sabya Avatar asked Jul 07 '09 13:07

Sabya


People also ask

How can change date format in jQuery ui Datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

What is minDate and maxDate in jQuery Datepicker?

If you like to restrict access of users to select a date within a range then there is minDate and maxDate options are available in jQuery UI. Using this you can set the date range of the Datepicker. After defining these options the other days will be disabled which are not in a defined range.

How do I change the default date in Datepicker?

$(". selector"). datepicker( {defaultDate:"+6"} );


2 Answers

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;
    }
});
like image 183
Simon Lieschke Avatar answered Sep 28 '22 02:09

Simon Lieschke


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

like image 44
w35l3y Avatar answered Sep 28 '22 00:09

w35l3y