Welcome to WebmasterWorld Guest from 54.196.214.35

Forum Moderators: open

Message Too Old, No Replies

jQuery to output empty select element

     
2:11 pm on Sep 23, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Dec 12, 2007
posts:233
votes: 0


Hi,
I have a jQuery script designed to output an html select element with decrementing years starting from the current year. Since I'm new to jQuery, can someone help me to find out why my script displays an empty select element?

this is the script:

var $annee = $('Année:&nbsp;<select id="annee"></select>');
var $date = new Date();
var intYear = $date.getFullYear();
var $options = '', i;
for(i = intYear ; i <= 2012; i--){
$options+='<option value="'+i+'">'+i+'</option>';
}
$annee.text($options);
4:29 pm on Sept 23, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Oct 17, 2005
posts: 4966
votes: 10


intYear is initialized to the current year (2013). Your for loop condition checks that i (set to intYear, aka 2013) is LESS THAN or EQUAL TO 2012. Since 2013 is not <= 2012, your loop body never executes.
6:36 am on Sept 24, 2013 (gmt 0)

Full Member

5+ Year Member

joined:Dec 12, 2007
posts:233
votes: 0


Thank you so much, it works!