user2935177 user2935177 - 7 months ago 10
Javascript Question

to generate years automatically in javascript dropdown

I created javascript that generates years used in my dropdown menu. I want to display current year and future years automatically. Does anyone know how to implement this?

<select id="selectElementId"></select>

<script>
var min = 2012,
max = 2021,
select = document.getElementById('selectElementId');

for (var i = min; i<=max; i++){
var opt = document.createElement('option');
opt.value = i;
opt.innerHTML = i;
select.appendChild(opt);
}
</script>

Answer

Use Date.getFullYear. Example:

var min = new Date().getFullYear(),
    max = min + 9

Fiddle: http://jsfiddle.net/t8fdh/