javascripthtmlbootstrap-daterangepicker

How to show an alert everytime date range picker dialog is opened?


I am trying to implement the date range picker in my web project. I have followed all the steps described in the documentation:

<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="" />

<script type="text/javascript">
    $(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('');
        });
    });
</script>


Solution

  • Use show.daterangepicker event for displaying alert. Here is a working code. I have added a function for displaying alert on show.daterangepicker event. I have used setTimeout so that the alert does not block the UI before the dropdown comes out.

    <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="" />
    
    <script type="text/javascript">
        $(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(''); 
            });
            
            $('input[name="datefilter"]').on('show.daterangepicker', function(ev, picker) {
                setTimeout(function(){
                    alert("You have opened datepicker");
                }, 0);
            });
            
        });
    </script>