I´m trying to add some functionality to a form with JQuery UI´s Datepicker.
When a user selects the arrival date (onSelect), a few things should happen:
I managed to get point 1 to work, but I´m a bit lost with the other features. If anyone can show me how to do this or get me on the right track that would be really great! I´m still a beginner but learning fast.
Here´s a Fiddle of what I have now: http://jsfiddle.net/mattvic/B6Kax/13/
Split the departure date in 3 and add each part to an input field
Looks like you already have the code written for this in the onSelect
handler of the destination datepicker. Unfortunately, it looks like setDate
does not trigger that event, and you cannot trigger it manually. What I would recommend is breaking that code out into a separate function that you can call from both places:
function splitDepartureDate(dateText) {
var depSplit = dateText.split("-", 3);
$('#alt-vertrek-d').val(depSplit[0]);
$('#alt-vertrek-m').val(depSplit[1]);
$('#alt-vertrek-y').val(depSplit[2]);
}
Then change your departure datepicker onSelect
handler to point to that function:
$('#vertrek').datepicker({
// Prevent selecting departure date before arrival:
beforeShow: customRange,
onSelect: splitDepartureDate
});
Lastly, call that function from your arrival's onSelect
event handler:
/* snip: */
// Populate departure date field
var nextDayDate = $('#aankomst').datepicker('getDate', '+3d');
nextDayDate.setDate(nextDayDate.getDate() + 3);
$('#vertrek').datepicker('setDate', nextDayDate);
// Manually call splitDepartureDate
splitDepartureDate($("#vertrek").val());
When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1
This is just a matter of using the onClose
event handler:
onClose: function() {
$("#vertrek").datepicker("show");
}
Here's your updated example: http://jsfiddle.net/zXMke/
Answering the title's question :
$("#firsDate").datepicker('option',"onClose", function() { $( "#secondDate" ).datepicker( "show" ); });
Source :
http://api.jqueryui.com/datepicker/#method-show
http://api.jqueryui.com/datepicker/#option-onClose
I recommend to use onSelect instead of onClose event in order to make sure that the 1st date has been selected before opening the second datepicker.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With