How to create a dynamically populate year in dropdownlist using javascript

dynamically populate year in dropdownlist using javascript

In this tutorial, you will learn how to create a dynamically populate year in javascript with an example.

In the HTML forms are need a dropdown with the select year for the date of birth, finance date between the date to pay the amount, etc...,

Lots of the websites have hardcoded the date and year values. Here is the solution instead of hardcoding quick and super easy to dynamically populate year with the range using javascript.

Read also:

Difference between double equals and triple equals in javascript

8 Best Free keyword Research Tools

Here, we are creating some basic HTML select tag <select> for id and the options get by the javascript using of javascript document.getElementById. 

 


Html



<select id="dateoption"></select>

Javascript Code



const dateoption = document.getElementById('dateoption');
let year =new Date().getFullYear();
let startyear = 2015;

while(year >= startyear){
let options = document.createElement('option');
options.text = year;
options.value = year;
dateoption.add(options);
year =year - 1;
}

dynamically populate year in dropdownlist using javascript
Previous Post Next Post