RussellHarrower RussellHarrower - 6 months ago 28
Javascript Question

jQuery generate years. for drop down

I came up with a small issue. that am trying to come up with a way to fix it.

Baisclly what I want to do is take 18 years of the year ie: 2011 which returns 1993.
and generate a drop down option for a user to select any year before that.

baisclly they have to show that they are over the legal age of 18.

If you can help that would be great.

Answer
var minOffset = 18, maxOffset = 100;

var thisYear = new Date().getFullYear();
var select = $('<select>');

for (var i = minOffset; i <= maxOffset; i++) {
    var year = thisYear - i;
    $('<option>', {value: year, text: year}).appendTo(select);
}

select.appendTo('body');

Demo: http://jsfiddle.net/DhpBg/.

Comments