I would like to bind to initialized datepicker "onSelect" function.
I've been trying hard to find a solution on the web, but was unsuccessful.
Anyone can tell me how to do it?
I would like to bind to initialized datepicker "onSelect" function.
I've been trying hard to find a solution on the web, but was unsuccessful.
Anyone can tell me how to do it?
You can simply use the setter
$('#datepicker').datepicker();
$('#datepicker').datepicker("option", "onSelect", function(){alert('hi')});
fiddle here http://jsfiddle.net/nhmsZ/
aristo theme demo
- jsfiddle.net/6pCMR
Mar 21, 2012 at 15:14
I found the above solutions caused problems in my case. Setting onSelect after the datepicker has already been initialised seems to cause the datepicker to be reinitialised. This may not be desirable.
If you are having problems it may be necessary to go through the back-door. This could have compatibility issues when JQUI is updated, but if you are willing to deal with that, then this code could help you out...
var inst = $.datepicker._getInst($('#startdate').get(0));
inst.settings.onSelect = function (dt) {
// your code here
};
First off Nicola's answer is correct and helped me. I was finding a few related posts that were not really solving my problem.
I'm writing this to expand on the existing answer. If you use a framework that limits your ability to control the order of execution of your JavaScript, say for example Zend Framework, you might need to fire off that bind option with a delay.
$(document).ready(function() {
function imahackfunction() {
$('#startdate').datepicker('option', 'onSelect', function () {
alert($('#startdate').val());
});
}
setTimeout(imahackfunction, 2000);
});
The above worked for me as I've got two $(document).ready(function() {
blocks, one I open in my view, the other "injected" by the framework which instantiates the datepicker.
What I wonder is why the datepicker does not simply fire the change
event on the input associated by the altField
property...
onselect
event? jqueryui.com/demos/datepicker/#event-onSelect . Or bind to it in the first place?$('#OfferStartDate').datepicker({ onSelect: function (dateText, inst) { console.debug("Validation"); } });
By "no luck" I mean that nothing is displayed on the console.bind to initialized "onSelect" function