Brandon Durham
Brandon Durham

Reputation: 7717

How can I have one datepicker change the date of another under certain conditions?

I have two datepickers on a page: Start Date and End Date.

$("#orders-start-date").datepicker({
    dateFormat: "yy-mm-dd",
    maxDate: new Date(),
    onSelect: function(date, el) {
        $this.trigger("orders:filter", date, $("#orders-end-date").val());
    }
}).datepicker("setDate", new Date());

$("#orders-end-date").datepicker({
    dateFormat: "yy-mm-dd",
    maxDate: new Date(),
    onSelect: function(date, el) {
        $this.trigger("orders:filter", $("#orders-start-date").val(), date);
    }
}).datepicker("setDate", new Date());

The End Date can't be earlier than the start date, so I need to change the Start Date to whatever the End Date is set to, but only if the End Date is set to an earlier date than Start Date.

Is this possible?

Upvotes: 0

Views: 118

Answers (1)

heymega
heymega

Reputation: 9391

Why don't you just limit the date you can pick using the data picker options? For instance

$("#orders-start-date").datepicker({
dateFormat: "yy-mm-dd",
onSelect: function(selected) {

     $("#orders-end-date").datepicker("option","minDate", selected)

}
});

Full working example http://jsfiddle.net/rvaldez/YdeY8/

Upvotes: 1

Related Questions