Last active
December 12, 2015 08:48
-
-
Save yevgenko/4746242 to your computer and use it in GitHub Desktop.
JavaScript helper function generates html options for days, months and years dropdowns
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
<div id="container"></div> |
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
// 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>'); | |
}); |
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
name: Birthday Options Helper | |
description: JavaScript helper function generates html options for days, months and years dropdowns | |
authors: | |
- Yevgeniy Viktorov | |
normalize_css: no |
Nice one. I've fixed some problems in a fork: https://gist.github.com/pvcarrera/7748082 Typo: Derember
and default parameters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://jsfiddle.net/gh/gist/jquery/1.9.1/4746242/