Patricia Patricia - 25 days ago 7
Javascript Question

JQuery datepicker - how to change the start and end dates after initialization?

I am working with someone else's code. In the initModule function, the code is initializing a start date along with other settings.

$('#StartDate').datepicker({
changeMonth: true,
changeYear: true,
showButtonPanel: true,
dateFormat: 'MM yy',
defaultStartDate: '-1m'
});


I have some radio buttons for Year, Quarter, and Month. When the user selects a radio button I call a findDefaultStartDate() function. For year, the default should be January of current year. Similar functionality for Quarter and Month.


I have the code to capture the correct default start date. However, I
have no idea how to change it since it was set in the initModule()
function.


Is there a way to change the defaultStartDate from another function in the JavaScript without changing any of the other settings that were initialized in the initModule() function?

Answer

You can use jQuery's setDate method like this:

$( "#StartDate" ).datepicker( "setDate", "10/12/2012" );

setDate - Sets the date for the datepicker. The new date may be a Date object or a string in the current date format (e.g., "01/26/2009"), a number of days from today (e.g., +7) or a string of values and periods ("y" for years, "m" for months, "w" for weeks, "d" for days, e.g., "+1m +7d"), or null to clear the selected date

Hope this helps!