Skip to content

Instantly share code, notes, and snippets.

@yevgenko
Last active December 12, 2015 08:48
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yevgenko/4746242 to your computer and use it in GitHub Desktop.
Save yevgenko/4746242 to your computer and use it in GitHub Desktop.
JavaScript helper function generates html options for days, months and years dropdowns
<div id="container"></div>
// Helper function generates html options for days, months and years dropdowns
function getBirthdayOptions(type, maxYear = 60, shiftYear = 0) {
var html = '';
switch (type) {
case 'days':
for (var i = 1; i < 32; i++) {
html += '<option value="' + (i) + '">' + i + '</option>';
}
break;
case 'months':
var theMonths = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Derember");
for (var i = 0; i < 12; i++) {
html += '<option value="' + (i+1) + '">' + theMonths[i] + '</option>';
}
break;
case 'years':
var currYear = new Date().getFullYear() - shiftYear;
var startYear = new Date().getFullYear() - maxYear;
while ( currYear >= startYear ) {
html += '<option value="' + currYear + '">' + currYear + '</option>';
currYear--;
}
break;
}
return html;
}
// Usage Example, with jQuery
$(document).ready(function () {
$('#container').html(
'<select name="month" class="first">' +
'<option value="" disabled="disabled" selected="selected">Month</option>' + getBirthdayOptions('months') +
'</select>' +
'<select name="day">' +
'<option value="" disabled="disabled" selected="selected">Day</option>' + getBirthdayOptions('days') +
'</select>' +
'<select name="year">' +
'<option value="" disabled="disabled" selected="selected">Year</option>' + getBirthdayOptions('years') +
'</select>');
});
name: Birthday Options Helper
description: JavaScript helper function generates html options for days, months and years dropdowns
authors:
- Yevgeniy Viktorov
normalize_css: no
@yevgenko
Copy link
Author

yevgenko commented Feb 9, 2013

@pvcarrera
Copy link

Nice one. I've fixed some problems in a fork: https://gist.github.com/pvcarrera/7748082 Typo: Deremberand default parameters

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