I have a very simple jQuery Datepicker calendar:
$(document).ready(function(){
$("#date_pretty").datepicker({
});
});
and of course in the HTML...
<input type="text" size="10" value="" id="date_pretty"/>
Today's date is nicely highlighted for the user when they bring up the calendar, but how do I get jQuery to pre-populate the textbox itself with today's date on page load, without the user doing anything? 99% of the time, the today's date default will be what they want.
You must FIRST call datepicker() > then use 'setDate' to get the current date.
OR chain your setDate method call after your datepicker initialization, as noted in a comment on this answer
It will NOT work with just
NOTE : Acceptable setDate parameters are described here
Assign the prettyDate to the necessary control.
This code will assure to use your datepicker's format:
No need to re-apply format, it uses the datepicker predefined-one by you on datepicker initialization (if you have assigned it!) ;)
The
setDate()
method sets the date and updates the associated control. Here is how:Demo
As mentioned in documentation,
setDate()
happily accepts the JavaScript Date object, number or a string:In case you are wondering, setting
defaultDate
property in the constructor does not update the associated control.Try this
You've got 2 options:
OPTION A) Marks as "active" in your calendar, only when you click in the input.
Js:
Css:
OPTION B) Input by default with today. You've to populate first the datepicker .