I\'ve created a date_select in Rails (which is 3 select boxes: one for year, one for month, and one for day).
To have 31st February on them is quite confusing. What
/* automatically update days based on month */ for (var i = 2014; i < 2019; i++) { $('').attr('value', i).text(i).appendTo('#start_year'); } function monthChanged() { debugger; var days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; var month = $('#start_month').val() - 1, year = +$('#start_year').val(); // Check for leap year if Feb if (month == 1 && new Date(year, month, 29).getMonth() == 1) days[1]++; // Add/Remove options if ($('#start_day option').length > days[month]) { // Remove $('#start_day option').slice(days[month] + 1).remove(); } else if ($('#start_day option').length < days[month] + 1) { // Add for (var i = $('#start_day option').length + 1; i <= days[month]; i++) { $('').attr('value', i).text(i).appendTo('#start_day'); } } } monthChanged(); // On document ready $('#start_month').change(monthChanged); // On month change $('#start_year').change(monthChanged); // On year change (for leap years)
Refer fiddle link: http://jsfiddle.net/u3fr1mt0/