user2935177 user2935177 - 1 year ago 69
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>

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;

Answer Source

Use Date.getFullYear. Example:

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


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download