Created
February 28, 2012 02:02
-
-
Save kpfefferle/1928544 to your computer and use it in GitHub Desktop.
jQuery: Limit day in Rails date_select based on month and year
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
$(function(){ | |
railsMonthDates(); | |
$("select[id*=_2i], select[id*=_1i]").change( railsMonthDates ); | |
}); | |
function railsMonthDates() { | |
$("select[id*=_2i]").each(function(){ | |
$monthSelect = $(this); | |
$daySelect = $(this).siblings("select[id*=_3i]"); | |
$yearSelect = $(this).siblings("select[id*=_1i]"); | |
var year = parseInt($yearSelect.val()); | |
var month = parseInt($monthSelect.val()); | |
var days = new Date(year, month, 0).getDate(); | |
var selectedDay = $daySelect.val() | |
$daySelect.html(''); | |
for(var i=1; i<=days; i++) { | |
$daySelect.append('<option value="'+i+'">'+i+'</option>'); | |
} | |
$daySelect.val(selectedDay); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing!