1

I need to set the default month and the year for the jquery monthpicker when page loads.

Below is my code. Currently monthpicker text box is empty when loads.

  $(document).ready(function() {      
    $('#monthPicker').datepicker( {
changeMonth: true,
changeYear: true,
showButtonPanel: true,
showButtonPanel: true,
dateFormat: 'MM yy',
onClose: function(dateText, inst) { 
    var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
    var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();

    $(this).datepicker('setDate', new Date(year, month, 1));
}
});
}); 

And also, I need to remove the dates from the calendar, I mean I just only need to display the year and month selection options.

EKBG
  • 243
  • 1
  • 8
  • 20

1 Answers1

0

If you want to show a default value when the page loads, just set the date after initializing the datepicker:

$(function() {
  $('#monthPicker').datepicker( {
    // ...
  });

  var default_date = new Date(2023, 10, 1);
  $("#monthPicker").datepicker("setDate", default_date);
});

Just set whatever date to default_date. Rembemer that, in javascripts, months start on 0 (0 means january, and 11 means december).

Wikiti
  • 1,626
  • 13
  • 21
  • I need to set it as current date. – EKBG Nov 09 '16 at 00:07
  • Could you explain to me what does *current date* mean to you? – Wikiti Nov 09 '16 at 00:09
  • Today date, now it is fixed. What I need now is removing dates from the calendar, keeping only the month and year selection. And, also I want to implement to stay on the last selected month, year when clicking the calendar for the second time. – EKBG Nov 09 '16 at 00:14