ling.s ling.s - 3 months ago 31
HTML Question

JQuery datepicker yearrange

I have 2 datepickers like this

<input type="text" class="datepicker" data-year-range="2010:2016"/>
<input type="text" class="datepicker" data-year-range="2000:2020"/>


I have to define the
yearRange
for the 2 datepickers in configuration. So, I did like this.

$('.datepicker').datepicker({
changeYear: true,
// $(this) isn't working here
//yearRange : $(this).attr('data-year-range'),
yearRange : $('.datepicker').attr('data-year-range')
});


But it is taking only
2010:2016
for the two datepickers. I don't understand why. The idea is to use different date ranges for the datepickers. Is it possible or do I need to define different datepickers?

Also, In datepickers is it possible to add the
yearRange
in html instead of the datepicker configuration?

Here is the fiddle

Answer

You can do it in following way:

https://jsfiddle.net/m5fvc22f/16/

$(document).ready(function() {
     $('.datepicker').each(function() {
       $(this).datepicker({
         changeYear: true,
         yearRange: $(this).data('year-range'),
       });
     })
   })