I have a datepicker which allows users to select two dates (check in and check out).
What I'm trying to do is highlight the selected date range on hover, so for example after the user has selected the check in date 08/03/2018 and is in the process of selecting the check out date, if they're hovering over 14/03/2018, all the dates between 08/03/2018 and 14/03/2018 would be highlighted.
I'm trying to create something similar to the datepicker on Kayak: https://www.kayak.co.uk/
Working code so far: https://jsfiddle.net/p1zpq9au/6/
Any help much appreciated!
jQuery(document).ready(function($) {
// Format the date
function dp_format_date(dateInput) {
var date_array = new Array();
date_array = dateInput.split('-');
var newDate = (date_array[2] + "/" + date_array[1] + "/" + date_array[0]);
return newDate;
}
function dp_check_in_onselect(date) {
// Set check in
if ( $("#check_in_alt").val() == '' && $("#check_out_alt").val() == '') {
$("#check_in").val( dp_format_date(date) );
$("#check_in_alt").val( date );
$("#check_out_alt").val('');
$(this).data('datepicker').inline = true;
}
// Set check out
else if ( $("#check_in_alt").val() != '' && $("#check_out_alt").val() == '') {
$("#check_out").val( dp_format_date(date) );
$("#check_out_alt").val( date );
$("#check_in").val( dp_format_date( $("#check_in_alt").val() ) );
$(this).data('datepicker').inline = false;
}
// Set check in
else {
$("#check_in").val( dp_format_date(date) );
$("#check_in_alt").val( date );
$("#check_out_alt").val('');
$(this).data('datepicker').inline = true;
}
}
// Load Datepicker Function
function dp_load_datepicker() {
jQuery("#check_in").datepicker({
minDate: new Date(),
numberOfMonths: 2,
dateFormat: 'yy-mm-dd',
firstDay: 1,
onSelect: dp_check_in_onselect,
});
}
dp_load_datepicker();
});