Created
August 17, 2011 11:46
-
-
Save steventux/1151384 to your computer and use it in GitHub Desktop.
ActionView date_select calendar logic in javascript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.observe("dom:loaded", function() { | |
var DAYS_IN_MONTH = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; | |
var dateEl = $('project_expiry_date_3i'); | |
var monthEl = $('project_expiry_date_2i'); | |
var yearEl = $('project_expiry_date_1i'); | |
if (dateEl && monthEl && yearEl) { | |
var currentDateIndex = dateEl.selectedIndex; | |
function leapYearAdjustedDays() { | |
var daysInMonth = DAYS_IN_MONTH[monthEl.selectedIndex]; | |
// Leap year calc. | |
var year = parseInt(yearEl.options[yearEl.selectedIndex].value); | |
if (monthEl.selectedIndex == 1 && year % 4 == 0) daysInMonth = 29; | |
return daysInMonth; | |
} | |
function adjustDays() { | |
dateEl.options.length = 0; | |
for (var idx = 1; idx <= leapYearAdjustedDays(); idx++) { | |
if (currentDateIndex == idx-1) | |
dateEl.options[idx-1] = new Option(idx,idx,false,true); | |
else | |
dateEl.options[idx-1] = new Option(idx); | |
} | |
} | |
monthEl.observe("change", adjustDays); | |
yearEl.observe("change", adjustDays); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment