0

I have a form where I want people to put in their year of birth. You can only apply if you are over 18 so in the year of birth field I want the min age to be 18. I have done this as shown below however I want to sort the numbers from latest year at the top and oldest year at the bottom. currently I show 1913 first, but I want to show 1994 first. Here is my code

<div id="my-container"></div>

<script>
      var d = new Date();
      var y = d.getFullYear();
      var selectList = "<select>";
      for (var x = (y-100); x < (y - 18); x++) {
          selectList += "<option>" + x + "</option>";
      }
      selectList += "</select>";
      $('#my-container').html(selectList);
      </script>
2
  • 2
    Switch the (y-100), (y-18), < sign, and ++ operator... For more information, look up for loops. Commented Feb 28, 2013 at 3:18
  • yes I tried something like that, but i didn't have the syntax right, I probably should have added this to the question. thanks Commented Feb 28, 2013 at 3:24

3 Answers 3

2

As Nile mentioned,

var d = new Date();
var y = d.getFullYear();
var selectList = "<select>";
for (var x = y-18; x >= y-100; x--) {
    selectList += "<option>" + x + "</option>";
}
selectList += "</select>";
$('#my-container').html(selectList);

http://jsfiddle.net/samliew/WzwKw/

Sign up to request clarification or add additional context in comments.

Comments

1

try this

 <script>
  var d = new Date();
  var y = d.getFullYear();
  var selectList = "<select>";
  for (var x = y-18; x > y - 100; x--) {
      selectList += "<option>" + x + "</option>";
  }
  selectList += "</select>";
  $('#my-container').html(selectList);
  </script>

Comments

0
<script>
    $( document ).ready( function() {
    var d = new Date();
    var y = d.getFullYear();
    var selectList = "<select>";
    for (var x = (y-18); x >= (y - 100); x--) {
      selectList += "<option>" + x + "</option>";
    }
    selectList += "</select>";
    $('#my-container').html(selectList);
    });
</script>

Please try with this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.