Saturday, 19 January 2019

Cancel SingleDate

I am using daterangepicker v3.0.3 and initializing all of my inputs that have the .date class to where they are single date pickers using the following code:

function checkDateInput() {
  var input = document.createElement('input');
  input.setAttribute('type','date');

  var notADateValue = 'not-a-date';
  input.setAttribute('value', notADateValue); 

  return (input.value !== notADateValue);
}

// Check if the browser supports the <input type="date" />
if (checkDateInput()) {
  // Change the types on the inputs that are dates
  $('.date').prop('type', 'date');
} else {
  // Iterate through each <input class="date" ... />
  $.each($('.date'), function(key, value) {
    $(value).daterangepicker({
      drops: "up",
      showDropdowns: true,
      singleDatePicker: true,
    });
  });
}
<!-- jquery -->
<script src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>

<!-- bootstrap -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>

<!-- daterangepicker -->
<script src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<script src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" rel="stylesheet"/>

<form>
  <fieldset>
    <legend>Dates</legend>
    <div class="form-group">
      <label for="date_written">Date Written</label>
      <input class="form-control date" id="date_written" name="date_written" required="required" />
    </div>
    <div class="form-group">
      <label for="effective_date">Effective Date</label>
      <input class="form-control date" id="effective_date" name="effective_date" required="required" />
    </div>
    <div class="form-group">
      <label for="cancellation_date">Cancellation Date</label>
      <input class="form-control date" id="cancellation_date" name="cancellation_date" />
    </div>
  </fieldset>
  <button type="submit" class="btn btn-success">Submit</button>
</form>

The problem that I'm running into is if I decide not to select a date and click outside the calendar, it will automatically input today's date when instead I'd prefer that it not insert anything at all. I assume that I need to set the autoUpdateInput to False, but I don't know how to manually update the input if a date was actually selected.



from Cancel SingleDate

No comments:

Post a Comment