RyK
RyK

Reputation: 33

JQuery DatePicker set default date on page load

I am using 2 JQueryUI Date Pickers for an application where I am using ajax to post the dates to a back end MySQL database. The first datepicker is used to post the initial date. The second datepicker is used to post the modified date.

I am having an issue when a user does not select a date from the date picker, in either instance. This is due to the onSelect function being used to retrieve the date value which is passed to ajax to post in the MySQL db.

I believe I need to define default date values for both datepickers when the page loads, so that if the user has not selected a date it will not be undefined when it gets passed to the AJAX to post.

What is the best way to do this?

<script>
$(function() {
$( ".datepicker" ).datepicker({
        inline: true,
        //nextText: '&rarr;',
        //prevText: '&larr;',
        showOtherMonths: true,
        dateFormat: 'yy-mm-dd',
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
        //showOn: "button",
        //buttonImage: "img/calendar-blue.png",
        //buttonImageOnly: true,
        onSelect: function(dateText, inst) {
            reservationDate = $(".datepicker[name=datepicker1]").val();
            modifyDate = $(".datepicker[name=datepicker2]").val();
            }
    });
});
</script>

<input type="text" class='datepicker' name="datepicker1" />
<input type="text" class='datepicker' name="datepicker2" />

Upvotes: 0

Views: 3704

Answers (2)

Jon
Jon

Reputation: 1234

Just create your inputs with the date that you want to set:

<input type="text" class="datepicker" name="datepicker1" value="Your date Here" />

jQuery UI datepicker also allows you to specify a default date.

Or in your AJAX request, replace the null value with the date you want to POST.

Upvotes: 0

Renato Todorov
Renato Todorov

Reputation: 560

The easiest way is putting the default date on the text field.

<input type="text" class='datepicker' name="datepicker1" value="01/01/2013" />

I don't know if this is the best on your scenario.

Upvotes: 1

Related Questions