We're using jquery UI 1.8.23 at my workplace.
How can I capture the date clicked in datepicker, so I can format the current date into a datestamp and pass that value into a hidden form field?
Here's some code I've tried to get working:
$('.ui-state-default').live('click', function(){
console.log('date clicked');
var currentdate = $(this).text();
var d = currentdate;
var m = monthFormat($('.ui-datepicker-month').text());
var y = $('.ui-datepicker-year').text();
$('a.ui-state-default').removeClass('ui-state-highlight');
$(this).addClass('ui-state-highlight');
if (currentdate.length < 2) {
d = "0" + currentdate;
}
var datestamp = y + "-" + m + "-" + d;
console.log(datestamp);
$('#dateHidden').val(datestamp);
});
Thanks for looking at my code I appreciate any help you guys can offer.
You don't need to do anything to format the date. The
DatePicker
widget does it for you. Just use thealtField
andaltFormat
properties:Documentation:
http://api.jqueryui.com/datepicker/#option-altField
http://api.jqueryui.com/datepicker/#option-altFormat
Demonstration:
http://jsfiddle.net/sFBn2/10/
jQuery
Datepicker
gives the option of formatting date. So why don't you make use of that?Also you don't need of a separate method to capture the click event. You the default method
onSelect
Check this JSFiddle
Your datepicker listens that.
see onSelect() function