Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

to generate years automatically in javascript dropdown [closed]

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>
like image 649
user2935177 Avatar asked Nov 01 '13 17:11

user2935177


1 Answers

Use Date.getFullYear. Example:

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

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

like image 131
Guilherme Sehn Avatar answered Sep 24 '22 01:09

Guilherme Sehn