Skip to content

Instantly share code, notes, and snippets.

@ldematte
Created April 6, 2015 14:39
Show Gist options
  • Save ldematte/7f71279df9d9a202d89d to your computer and use it in GitHub Desktop.
Save ldematte/7f71279df9d9a202d89d to your computer and use it in GitHub Desktop.
Hacking datepicker to get notification of any change to the underlying date object
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Datepicker - Select on every date change</title>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.3/jquery-ui.js"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker();
//PF
$('#datepicker').datepicker("option", "onSelect", function(dateStr, inst) {
if (inst.input.val() !== "" && inst.input.val() !== inst._lastUpdate) {
inst._lastUpdate = dateStr;
console.log(dateStr);
}
});
var previousUpdate = $.datepicker._updateDatepicker;
var _update = function(inst) {
var dateStr = $.datepicker._formatDate(inst);
var onSelect = $.datepicker._get(inst, "onSelect");
if (onSelect) {
// trigger custom callback
onSelect.apply((inst.input ? inst.input[0] : null), [dateStr, inst]);
}
previousUpdate.apply($.datepicker, [inst]);
}
$.datepicker._updateDatepicker = _update;
});
</script>
</head>
<body>
<p>Date: <input type="text" id="datepicker"></p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment