Yogesh Gupta
Yogesh Gupta

Reputation: 170

Setting yearRange of datepicker from javascript variable

$(".parent_div").delegate(".input_control", "focusin", function () {
    var year = $('#this_year_input').val();
    var year_text = "'" + year + ":" + year + "'";
    $(this).attr("readonly", "true");
    $(this).datepicker({
        "dateFormat": 'mm/dd/yy',
            "changeMonth": true,
            "changeYear": true,
            "yearRange": year_text
    });
});

I am trying to restrict user by selecting dates for the year he is selecting. Reason is I have multiple datepicker controls and I need the same year that user selected once. So I provided dropdown of year and getting value from it in variable 'year'. Now this is what happens. Problem is that if 'this_year_input' has 2010, var year_text get '2010:2010' but the it doesn't work in calender control. If I replace year_text with '2010:2010' in argument, it works

Upvotes: 0

Views: 1206

Answers (3)

Vignesh Subramanian
Vignesh Subramanian

Reputation: 7289

You can set it like this

$(function() {
    var minYear=1990;
    var maxYear=2000;
    $( "#datepicker" ).datepicker({ 
        yearRange: minYear+':'+ maxYear,
        changeYear: true

    });
});

CHECK THIS LIVE JSFIDDLE DEMO

Upvotes: 0

Read yearRange

var year_text = year + ":" + year ;

or

"yearRange":  year + ":" + year

Upvotes: 0

GautamD31
GautamD31

Reputation: 28763

You can give like

$(this).datepicker({
    "dateFormat": 'mm/dd/yy',
        "changeMonth": true,
        "changeYear": true,
        "yearRange": year + ":" + year
});

Upvotes: 2

Related Questions