Search code examples
jqueryjquery-uijquery-ui-datepickerdaterangepicker

jQuery UI Datepicker Problem with arrival - depart dates


Argh.. I am stumped....

I have a reservation module that is running the jQuery date-picker UI. I have an arrival date field and a departure date field each with a date-picker object initiated on both.

I want the default arrival date to be todays date I achieved this by doing the following:

   <script type="text/javascript">
      $(document).ready(function(){

          $(".arrive").datepicker();
          $(".depart").datepicker();

          $(".arrive").datepicker('setDate', new Date());

      });
   </script> 

That works perfectly however when a user selects an arrival date I then want the departure default date to be set as one day after the users selected arrival date. I tried using the onSelect events for the date-picker object and tried to pull the value from the arrival field and add a day but to no avail. I have seen this functionality before, I don't think it is very difficult I am just beyond stumped on this one. Any ideas? My html for the text fields is as follows:

<ul>
  <li>  
    <label>Arriving</label>     
    <span class="dateinput">                                                   
        <input class="arrive" type="text" value="" name="startDate" />                                  
        <img id="arrive"  src="images/calendar_icon.png"  />
        <div class="clear"></div>
    </span>
  </li>

  <li>  
    <label>Departing</label>        
    <span class="dateinput">                                
        <input class="depart" name="endDate" value="" type="text"  />               
        <img src="images/calendar_icon.png" />
        <div class="clear"></div>
    </span> 
  </li>
</ul>

Thanks a bunch to whoever can help me with this issue.


Solution

  • Using the documentation as inspiration:

    <script type="text/javascript">
      $(document).ready(function(){
    
          $(".arrive").datepicker({
              onSelect: function( selectedDate ) {
                // Parse the selected date
                var instance = $( this ).data( "datepicker" ),
                    date = $.datepicker.parseDate(
                        instance.settings.dateFormat ||
                        $.datepicker._defaults.dateFormat,
                        selectedDate, instance.settings );
    
                // Add one day
                date.setDate(date.getDate()+1);
    
                // Set the new date
                $(".depart").datepicker('setDate', date);               
            }
          });
          $(".depart").datepicker();
    
          $(".arrive").datepicker('setDate', new Date());
      });
    </script>