Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - how to alter Datepicker settings after it has been initialized

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?

like image 390
kubal5003 Avatar asked Mar 21 '12 14:03

kubal5003


People also ask

How do I change Datepicker format?

Do one of the following: For a text box control or a date picker control, ensure that the Data type list displays the appropriate data type, and then click Format. For an expression box control, ensure that the Format as list displays the appropriate data type, and then click Format.


2 Answers

You can simply use the setter

$('#datepicker').datepicker();
$('#datepicker').datepicker("option", "onSelect", function(){alert('hi')});

fiddle here http://jsfiddle.net/nhmsZ/

like image 164
Nicola Peluchetti Avatar answered Oct 19 '22 20:10

Nicola Peluchetti


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
};
like image 33
xtempore Avatar answered Oct 19 '22 20:10

xtempore