Reputation: 7254
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?
Upvotes: 12
Views: 15997
Reputation: 5505
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
};
Upvotes: 1
Reputation: 7053
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...
Upvotes: 0
Reputation: 76880
You can simply use the setter
$('#datepicker').datepicker();
$('#datepicker').datepicker("option", "onSelect", function(){alert('hi')});
fiddle here http://jsfiddle.net/nhmsZ/
Upvotes: 23