mattias mattias - 1 month ago 14
Javascript Question

Set jQuery datepicker minDate depending on input

I'm trying to make a jQuery datepicker set

minDate
depending on the value of a radio button.

When I try to write a function returning a string, my JavaScript keeps crashing. Any ideas on why this is not working?

$("#datepicker").datepicker({
changeMonth: true,
changeYear: true,
yearRange: "-35:+0",
dateFormat: "yy-mm-dd",
minDate: function() {
var str = "-35Y";
if($("#inputfield").val() == "value") {
str = "-33Y";
}
return str;
}
});

Answer

jsFiddle

This works fine.... you need to make sure that you have inputfield with a value

 <input type='text' id='datepicker'></div>
 <input type='text' value='value' id='inputfield'>

$("#datepicker").datepicker({
    changeMonth: true,
    changeYear: true,
    yearRange: "-35:+0",
    dateFormat: "yy-mm-dd",
    minDate:mdate()
});


function mdate(){
        var str = "-35Y";
        if($("#inputfield").val() == "value"){
            str = "-33Y";
        }
        return str;
}
  • Edit:*

    $('#inputfield').on('change',function(){ $('#datepicker').datepicker('option', 'minDate', mdate()); });

Comments