5

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:

  1. Update the departure to 3 days after the arrival date (This works now :))
  2. Split the departure date in 3 and add each part to an input field (see arrival date example in Fiddle)
  3. When the arrival-datepicker closes (onClose), the departure-datepicker should open automatically, defaulting to the date selected in point 1 (example: http://www.kayak.es )

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/

Jonas
  • 109,920
  • 93
  • 295
  • 369
Mattvic
  • 426
  • 8
  • 16

3 Answers3

3

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/

Andrew Whitaker
  • 121,982
  • 31
  • 284
  • 303
  • 1
    Thanks so much Andrew, this was exactly what I was looking for. – Mattvic Jun 26 '11 at 18:18
  • @Mattvic: No problem. Interesting/unfortunate that the `setDate` method doesn't trigger `onSelect`... – Andrew Whitaker Jun 26 '11 at 18:19
  • There is a problem with the show on close. In the fiddle, try to select the next month range in the second calendar. Note that you need to click the month twice before it switches. I would love to know how to fix this! – Tim Jun 18 '15 at 22:28
  • Wrapping the show call in a timeout seems to fix the next month problem. I don't exactly know why. – Tim Jun 18 '15 at 22:46
2

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.

bensiu
  • 22,720
  • 51
  • 71
  • 112
0

point 3 answer:

$(function() {
  $( '#aankomst' ).datepicker({
    onClose: function(dateText, instance) {
      $( '#vertrek' ).datepicker('show');
     },      
    onSelect: function( dateText, instance ) {

      // Split arrival date in 3 input fields                        
      var arrSplit = dateText.split("-");
        $( '#alt-aankomst-d' ).val(arrSplit[0]);
        $( '#alt-aankomst-m' ).val(arrSplit[1]);
        $( '#alt-aankomst-y' ).val(arrSplit[2]);

       // Populate departure date field
       var nextDayDate = $( '#aankomst' ).datepicker('getDate', '+3d');
         nextDayDate.setDate( nextDayDate.getDate() + 3 );
           $( '#vertrek' ).datepicker( 'setDate', nextDayDate );
           }                
        });
}); 

you already have the solution for point 2?

Cheers, Daddy

whosrdaddy
  • 11,540
  • 4
  • 44
  • 95