Skip to content

Instantly share code, notes, and snippets.

@danwill
Last active January 9, 2023 04:15
Show Gist options
  • Save danwill/4677782 to your computer and use it in GitHub Desktop.
Save danwill/4677782 to your computer and use it in GitHub Desktop.
Javascript: Date of birth form control population #snippet
var monthList, startYear;
monthList = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
$('select[name$="dob_month"]').each(function() {
for (var i = 0; i < monthList.length; i = i+1) {
$(this).append('<option value="'+ String(i) +'">' + String(monthList[i]) + '</option>');
}
});
$('select[name$="dob_day"]').each(function() {
for (var j = 1; j <= 31; j = j+1) {
$(this).append('<option value="'+ j +'">' + j + '</option>');
}
});
// Only lists possible years based on a target age of 21
startYear = new Date();
startYear = startYear.getFullYear() - 21;
$('select[name$="dob_year"]').each(function() {
for (var k = startYear; k >= 1900; k = k-1) {
$(this).append('<option value="'+ k +'">' + k + '</option>');
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment