How to dynamically change the datepicker range?

Discussion in 'jQuery' started by Heartstrings, Mar 16, 2020.

  1. #1
    Datepicker library I'm using: GIJGO

    I'm trying to change the minDate inputs of the datepicker library depending on which option is selected by user is a drop-down menu. The options of the drop-down menu are as follows:

    <select class='form-control mr-2' id='idType' name='idType' placeholder='Select' required>
    <option>UserID</option>
    <option>UID</option>
    <option>All Records</option>";
    </select>
    HTML:
    I am trying to set the minDate of the datepicker to be 2 months from today if "All Records" is selected. Otherwise the minDate will be '2019-11-18'. The datepicker is stuck on '2019-11-18' and will not change to 2 months prior with my if statement. What am I doing wrong?


    $(document).ready(function() {
    
    var today = moment().format('YYYY-MM-DD');
    var pastMo = moment().subtract(2, 'M').format('YYYY-MM-DD');
    
    getDatePicker('2019-11-18', today);
    
    console.log(pastMo);
    
    $('#idType').change(function(){
       if($(this).val() == 'All Records'){
          $("#idSearch").prop("disabled", true);
          getDatePicker(pastMo, today);
        }
       else {
          $("#idSearch").prop("disabled", false);
          getDatePicker('2019-11-18', today);
       }
     });
    });
    function getDatePicker(min, today) {
    $('#start_datepicker').datepicker({uiLibrary: 'bootstrap4',format: 'yyyy-mm-dd', minDate: min, maxDate: today});
    $('#end_datepicker').datepicker({uiLibrary: 'bootstrap4',format: 'yyyy-mm-dd', minDate: min, maxDate: today});
    }
    Code (JavaScript):
    Thank you for any tips or suggestions!
     
    Heartstrings, Mar 16, 2020 IP