Reputation: 6800
I came up with a small issue, I am trying to come up with a way to fix it.
Basically what I want to do is take 18 years off the year ie: 2011 which returns 1993, and generate a drop down option for a user to select any year before that.
Basically they have to show that they are over the legal age of 18.
Upvotes: 2
Views: 14362
Reputation: 93664
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');
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
Demo: http://jsfiddle.net/DhpBg/.
Upvotes: 5
Reputation: 37
var minOffset = 0, maxOffset = 100; // Change to whatever you want // minOffset = 0 for current year
var thisYear = (new Date()).getFullYear();
var m_names = ['January', 'February', 'March','April', 'May', 'June', 'July','August', 'September', 'October','November', 'December'];
var month = 0 // month = (new Date()).getMonth(); // for cuurent month
for (var j = month; j <= 11; j++) {var months = m_names[ 0 + j].slice( 0, 3 ); $('<option>', {value: months, text: months}).appendTo(".month"); }
for (var i = minOffset; i <= maxOffset; i++) { var year = thisYear + i; $('<option>', {value: year, text: year}).appendTo(".year");}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select class="year">
</select>
<select class="month">
</select>
better solution for month and year by jquery
www.jsfiddle.net/sudharshanreddyjanike/byvotshz/2/
Upvotes: 4
Reputation: 145
<select name="Year" class="bdayyear">
<option value="year">Year</option>
<script language="javascript">
for (var i = 1960; i < 2010; i ++) {
document.write("<option value=\"" + i + "\">" + i + "</option>\n");
}
</script>
</select>
I hope that helps because that is pretty much how to do it.
Upvotes: 2
Reputation: 5302
You can do this in native Javascript without any jQuery pretty easily.
Demo: http://jsbin.com/orasa5/2/
Edit: Not sure why I was creating a node for every option. It's early in the morning, I guess.
Upvotes: 2