I'm using this jQuery datepicker and I'm trying to get the value of the date on hover. I see the plugin has a parameter:
eventName
The desired event to trigger the date picker. Default: 'click'
Since the documentation is very limited, I wonder if there're are other options besides click and if not, how can I use eventName
to get the value on hover.
The eventName
option is only used to bind the internal show
method to an event:
$(this).bind(options.eventName, show);
You could, in theory, use hover
to show the datepicker but you'd have to specify 'mouseenter mouseleave'
for the eventName
option as hover
is a jQuery shortcut method to bind to 'mouseenter mouseleave'
.
Since you've stated (in comments) that you want the behavior found at kayak.co.in, you can mimic this merely by chaining a mouseenter
handler after the .DatePicker
call (no change needed to anything else):
$('#date').DatePicker({
// options...
}).on('mouseenter', '.datepickerDays td:not(.datepickerDisabled, .datepickerNotInMonth)', function (e) { // delegating on the mouseenter event which jQuery patches to be a cross-browser event, and only targeting clickable dates
var target = $(this).children('a'), // cache lookup
options = target.parents('.datepicker').data('datepicker'), // get DatePicker options
current = new Date(options.current), // grab the current month/year
val = parseInt(target.text(), 10), // grab the target date
hoverVal = new Date(current.getFullYear(), current.getMonth(), val), // make into an actual date
hoverDay = hoverVal.getDayName(), // get the short day name
hoverM = hoverVal.getMonth() + 1, // and month
hoverD = hoverVal.getDate(), // and date
hoverText = hoverDay + ' ' + (hoverD < 10 ? '0' + hoverD : hoverD) + '/' + hoverM, // for a formatted text value
selectedVal = new Date(options.date[0]), // get the selected date (which may just be the initial date without an actual selection or an actual selected date, hereafter "selected")
selectedDay = selectedVal.getDayName(), // get the short day name
selectedM = selectedVal.getMonth() + 1, // and month
selectedD = selectedVal.getDate(), // and date
selText = selectedDay + ' ' + (selectedD < 10 ? '0' + selectedD : selectedD) + '/' + selectedM, // for a formatted text value
startDate = $('#startDate').data('lastHovered') || new Date(hoverVal) || selectedVal, // default to: last hovered, current hover, or "selected" date (in that order)
endDate = $('#endDate').data('lastHovered') || new Date(options.date[1]) || startDate, // default to: last hovered, actual selected date, or "selected" date (in that order)
startDateSelected = $('#startDate').data('startDateSelected') || $('.datepickerDays .datepickerSelected.first').length > 0, // test whether an actual date was selected
endDateSelected = !isNaN(options.date[1]) && (options.date[1] - options.date[0] > 86400000), // test whether an end date was selected. end date is set in options when start date is actually selected and is the same day as the selected start date but the time is set to 23:59:59
selector; // variable to store a selector string
// no end date has been selectd AND if no start date has been selected, or if it has and the user is hovering over an earlier date, or if the user hasn't selected a date yet
if (!endDateSelected && (!startDateSelected || (startDateSelected && hoverVal < selectedVal) || hoverVal <= startDate)) {
selector = '#startDate'; // use the startDate input
$('#endDate').val(''); // since using startDate, endDate has not been selected. make sure the input is cleared.
} else if (!endDateSelected){ // otherwise, as long as no end date has been selected
selector = '#endDate'; // use the endDate input
$('#startDate').val(selText); // and set the value of startDate back to the actual selected date value
}
if (!endDateSelected) { // if the user hasn't picked an end date (which cannot be picked without a start date)
$(selector).data({ // persist the last hovered date and whether a start date has been selected
"lastHovered": hoverVal,
"startDateSelected": startDateSelected // this is necessary as the plugin routinely destroys and recreates the tables that make up the calendars while navigating the calendars
}).val(hoverText); // set the value of the input to the hovered date
}
});
Try this approach:
Create a Div for your hover target
<div id="content">
<div id="test">Hover Me</div>
<div id="datePicker"></div>
</div>
On ready, initialize and bind to hover event to open
$(function () {
$('#datePicker').datepicker();
$('#datePicker').hide();
$('#content').hover(function () {
$('#datePicker').fadeIn('fast');
}, function () {
$('#datePicker').fadeOut('fast');
});
});
Fiddle (couldnt reference your picker so i used JQuery UI)
I applied my approach to your Fiddle. Now YOUR DatePicker appears when you hover over "Hover Me" and disappears whencursor leaves. I added:
$(function () {
$('#date').hide();
$('#test').hover(function () {
$('#date').show();
}, function () {
$('#date').hide();
});
});
-
BTW, I made the DatePicker Dialog disappear on non-hover just for effect. That can easily be changed by removed the adjoining
}, function () {
$('#date').hide();
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