is there a jquery dropdown year selector
Solution 1
What about just:
<select name="yearpicker" id="yearpicker"></select>
And then:
for (i = new Date().getFullYear(); i > 1900; i--)
{
$('#yearpicker').append($('<option />').val(i).html(i));
}
Solution 2
Wrapping around the code into jQuery plugin to select years between start and end year:
jQuery.extend(jQuery.fn,{
/*
** Year selection plugin
** Sample usage
** $('#yearpicker').years(2007, 2017);
**
** HTML
** <select name="yearpicker" id="yearpicker"></select>
*/
years: function(start, end) {
for (i = start; i <= end; i++)
{
$(this).append($('<option />').val(i).html(i));
}
}
});
Solution 3
Expanding on Jimmy Sawczuk's solution - what if you also needed to process feedback data into a form page where a previous selection needed to be loaded/selected.
A variable holding the user's previous selection can be used in an if statement to dynamically assign the "Selected" attribute like so:
for (i = new Date().getFullYear(); i > 1900; i--)
{
if(userselection == i){
$('#yearpicker').append($('<option />').val(i).attr("selected","selected").html(i));
} else {
$('#yearpicker').append($('<option />').val(i).html(i));
}
}
Related videos on Youtube
Nicola Peluchetti
I've 10 years of full stack development experience: 6 with important agencies ( 10up, Webscience, Politecnico ), 3 with a startup and 1 freelancing. In the last year I worked on a huge migration from a static/PHP site to WordPress, on a React Native App ( yes, a social network, 2018), on a React search page backed by Elastic Search, on a WP REST api for a React Back End and more. My main expertise is in the WordPress environment ( Woocommerce and WordPress VIP experience too ), but I also have production experience with React, React Native, Java, node.js, Elastic Search, Zend Framework, Drupal 8, SQL, Saas, Less, Backbone, Webpack, MQL4 and Grunt. I'm a fast learner and prefer to work in places where you can code in multiple languages. I'm looking for remote contract work in the US Eastern timezone. I've been in the top 100 on StackOverflow twice: number 63 on the site in July 2011 and number 73 in March 2012 http://stackexchange.com/leagues/1/month/stackoverflow/2012-03-01/397861?sort=reputationchange#397861
Updated on July 09, 2022Comments
-
Nicola Peluchetti almost 2 years
is there a jQuery plugin that creates automatically a dropdown year selector (that is a "select" element poulated with all years starting from current and dating back to a given year)?
I don't need day/month (otherwise i'd have used the datepicker), i just need the year!
Thanks in advance
-
Richard Neil Ilagan about 13 yearsNot everything has to be a plugin (though, I must admit, that can be helpful in itself). +1
-
Nicola Peluchetti about 13 yearsIn the end i implemented a "server side version" of your idea because a also needed to populate it, but it was excatly what i needed!thx again
-
black_belt over 12 yearsHi, Jimmy, could you please tell me how to use same year picker in Codeigniter's default select dropdown- Codeigniter's dropdown select is liek this>> <? echo form_dropdown('salary_month', $options,'', 'class="medium required"' );?> Thanks in Advance :)
-
Jimmy Sawczuk over 12 years@Srijon that's sort of outside the scope of this question, you should ask it as a new question.
-
black_belt over 12 years@Jimmy Sawczuk .. sorry my mistake .:-)
-
Tracker1 over 12 yearsI find that some browsers don't like the self closing tags, so will favor '[option][/option]' (replacing the brackets of course).