3

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

2 Answers 2

7

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'),
       });
     })
   })
4
  • @ZakariaAcharki - no...this is working one. please try to run another solution and you will come to know what I am saying.
    – vijayP
    Aug 19, 2016 at 9:46
  • @ZakariaAcharki - have you open the date picker and seen the range there in your fiddle? please do it.
    – vijayP
    Aug 19, 2016 at 9:49
  • 1
    @ZakariaAcharki - this one actually works, the other one doesn't (at time of writing). Go ahead, try them both in JSFiddle Aug 19, 2016 at 9:49
  • @ZakariaAcharki - I am waiting for your comments. The other person just updated the answer...;)!
    – vijayP
    Aug 19, 2016 at 9:55
1

Try this : use $(this) instead of $('.datepicker')

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

JSFiddle Demo

0

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.