Bootstrap Daterange Picker – Clear Selection after click apply button

Issue

I am using http://www.daterangepicker.com/#usage package and currently facing some difficulties in clearing the selection.
Based on the documentation, there is a clear option but it doesn’t work/cleared after I click to apply button.

This is the codes:

$(function() {
  $('input[name="datefilter"]').daterangepicker({
      autoUpdateInput: false,
      locale: {
          cancelLabel: 'Clear'
      }
  });

  $('input[name="datefilter"]').on('apply.daterangepicker', function(ev, picker) {
      $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
  });

  $('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) {
      $(this).val('');
  });

});

Does anyone have the same experience? how to solve this issue? looks like this package is not maintained anymore 🙁

Solution

Try resetting startDate and endDate

picker.setStartDate({})
picker.setEndDate({})
$('input[name="datefilter"]').daterangepicker({
  autoUpdateInput: false,
  locale: {
    cancelLabel: 'Clear'
  }
});

$('input[name="datefilter"]').on('apply.daterangepicker', function(ev, picker) {
  $(this).val(picker.startDate.format('MM/DD/YYYY') + ' - ' + picker.endDate.format('MM/DD/YYYY'));
});

$('input[name="datefilter"]').on('cancel.daterangepicker', function(ev, picker) {
  $(this).val('')
  picker.setStartDate({})
  picker.setEndDate({})
});
<script type="text/javascript" src="https://cdn.jsdelivr.net/jquery/latest/jquery.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/daterangepicker/daterangepicker.css" />

<input type="text" name="datefilter" value="01/01/2018 - 01/15/2018" />

Also, set the default date format as ‘MM/DD/YYYY’. Don’t need apply.daterangepicker event

Answered By – User863

Answer Checked By – Marilyn (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.