Skip to content

Instantly share code, notes, and snippets.

@kpfefferle
Created February 28, 2012 02:02
Show Gist options
  • Select an option

  • Save kpfefferle/1928544 to your computer and use it in GitHub Desktop.

Select an option

Save kpfefferle/1928544 to your computer and use it in GitHub Desktop.
jQuery: Limit day in Rails date_select based on month and year
$(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);
});
}
@nickgnd
Copy link
Copy Markdown

nickgnd commented Jun 28, 2016

Thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment