add years as options to select dropdown

add the years as dropdown items between a range of years with javascript

document ready js only version

HTML

<select id="years" name="years">
    <option value="-1">&nbsp;</option>
</select>

Javascript

// this also needs a document ready check, otherwise it cant find the element to add the years to.
document.addEventListener("DOMContentLoaded", function(event) { 
  // add dom loaded stuff

var selectBox = document.getElementById('years');
for (var i = 2030; i >= 1980; i--) {
    var option = document.createElement('option');
    option.value = i;
    option.innerHTML = i;
    selectBox.appendChild(option);
}
  
});

Other Items in javascript

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 233 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

โค๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ•น

Random Quote
โ€œWe ought to take outdoor walks in order that the mind may be strengthened and refreshed by the open air and much breathing.".
Seneca