Pablo Fernandez
Pablo Fernandez

Reputation: 287450

jQuery UI Calendar widget to change date when changing year or month

I'm using the jQuery UI calendar widget with the month and year drop downs, like show here: http://jqueryui.com/demos/datepicker/#dropdown-month-year

Apparently some people pick a day, then open the widget again, pick a year and a month and let it close. The issue is that picking the year and month doesn't change the year and month of the date being entered, only the one being displayed to the user.

Is there a way to have the year and month have an immediate effect on the date, so that if I have the date 22/06/2012 and chose 1990, it'll automatically go to 22/06/1990.

Upvotes: 0

Views: 1222

Answers (1)

kpotehin
kpotehin

Reputation: 1035

You should set trigger to this event onChangeMonthYear.

For example:

$(function() {
    $( "#datepicker" ).datepicker({
        changeMonth: true,
        changeYear: true,
        onChangeMonthYear: function(year, month, inst) {
        if(inst.currentYear != inst.drawYear || inst.currentMonth != inst.drawMonth) {
          $(this).datepicker("setDate", new Date(year, month - 1, inst.selectedDay));
        }
    });
});

Upvotes: 1

Related Questions