Reputation: 928
Maybe I am misunderstanding some of the stack answers and the docs them seleves but I created a set of date pickers in a modal, that upon initialization:
var startDate;
$(".datepicker .startDate").datepicker({
onSelect : function(){
startDate = $(self).datepicker('getDate');
}
});
console.log(startDate);
$(".datepicker .endDate").datepicker();
The .datepicker .startDate
should return me the date selected and log it to the console, instead I get nothing - the date picker works. but theirs nothing logged to the console ...
Ideas?
Upvotes: 0
Views: 3482
Reputation: 141
Maybe just a syntax issue, here is a simple example: http://jsfiddle.net/RZxmP/11/
<div id="datepicker" clss="datepicker startdate"></div>
var startDate;
$(document).ready(function(){
$( "#datepicker" ).datepicker({
onSelect : function(){
startDate = $(this).datepicker('getDate').getDate();
console.log(startDate);
}
});
});
Upvotes: 0
Reputation: 11671
The value of the datepicker is set when selecting the date. The onSelect
callback is called when selecting the date. In your code you are printing before selecting the date so you will not see any value printed.
If you want to get and use the value you need to do that within the onSelect
callback function.
http://api.jqueryui.com/datepicker/#option-onSelect
For example the code below will alert the selected value,
var startDate;
$(".datepicker .startDate").datepicker({
onSelect: function (e) {
alert(e);//the value
startDate = $(this).datepicker('getDate');
alert(startDate);//the value
}
});
console.log(startDate);//this will print nothing
HTML from fiddle
<div class="datepicker">
<input type="text" class="startDate" class="hasDatepicker" />
</div>
js from fiddle
var startDate;
$(".datepicker .startDate").datepicker({
onSelect: function (e) {
alert(e);
startDate = $(this).datepicker('getDate');
alert(startDate);
}
});
console.log(startDate);
$(".datepicker .endDate").datepicker();
Upvotes: 1