i’m new here & i’m a noob with javascript, so please, be gentle & detailed 🙂
i snagged this script (link) to allow users to select a whole week from a calendar. it works perfectly as-is, as it should.
my problem is, it puts the selected week results as inline content in a SPAN tag.
i really need the results to be put into a FORM Hidden INPUT value so it can be submitted with the form it’ll be in… automatically.
i have researched & researched for 2 whole days; read as much as i could about jquery, etc. but i’m just not “getting it”.
would really appreciate some help.
THANKS!!!
<head>
<script type="text/javascript">
$(function() {
var startDate;
var endDate;
var selectCurrentWeek = function() {
window.setTimeout(function () {
$('.week-picker').find('.ui-datepicker-current-day a').addClass('ui-state-active')
}, 1);
}
$('.week-picker').datepicker( {
dateFormat: 'yy-mm-dd',
showOtherMonths: true,
selectOtherMonths: true,
onSelect: function(dateText, inst) {
var date = $(this).datepicker('getDate');
startDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay());
endDate = new Date(date.getFullYear(), date.getMonth(), date.getDate() - date.getDay() + 6);
var dateFormat = inst.settings.dateFormat || $.datepicker._defaults.dateFormat;
$('#startDate').text($.datepicker.formatDate( dateFormat, startDate, inst.settings ));
$('#endDate').text($.datepicker.formatDate( dateFormat, endDate, inst.settings ));
selectCurrentWeek();
},
beforeShowDay: function(date) {
var cssClass = '';
if(date >= startDate && date <= endDate)
cssClass = 'ui-datepicker-current-day';
return [true, cssClass];
},
onChangeMonthYear: function(year, month, inst) {
selectCurrentWeek();
}
});
$('.week-picker .ui-datepicker-calendar tr').live('mousemove', function() { $(this).find('td a').addClass('ui-state-hover'); });
$('.week-picker .ui-datepicker-calendar tr').live('mouseleave', function() { $(this).find('td a').removeClass('ui-state-hover'); });
});
</script>
</head>
<body>
<div class="week-picker"></div>
<p><label>Week :</label>
<span id="startDate"></span> - <span id="endDate"></span>
</body>
This should get you started. (or here on fiddle )
Just change the type to hidden, I only set it to text so you can easily see it’s working.