bootstrap datepicker change date event doesnt fire up when manually editing dates or clearing date

133,480

Solution 1

You have to use the change event on the input itself if you want to respond to manual input, because the changeDate event is only for when the date is changed using the datepicker.

Try something like this:

$(document).ready(function() {
    $('.datepicker').datepicker({
        format: "yyyy-mm-dd",
    })
    //Listen for the change even on the input
    .change(dateChanged)
    .on('changeDate', dateChanged);
});

function dateChanged(ev) {
    $(this).datepicker('hide');
    if ($('#startdate').val() != '' && $('#enddate').val() != '') {
        $('#period').text(diffInDays() + ' d.');
    } else {
        $('#period').text("-");
    }
}

Solution 2

In version 2.1.5

changeDate has been renamed to change.dp so changedate was not working for me

$("#datetimepicker").datetimepicker().on('change.dp', function (e) {
            FillDate(new Date());
    });

also needed to change css class from datepicker to datepicker-input

<div id='datetimepicker' class='datepicker-input input-group controls'>
   <input id='txtStartDate' class='form-control' placeholder='Select datepicker' data-rule-required='true' data-format='MM-DD-YYYY' type='text' />
   <span class='input-group-addon'>
       <span class='icon-calendar' data-time-icon='icon-time' data-date-icon='icon-calendar'></span>
   </span>
</div>

Date formate also works in capitals like this data-format='MM-DD-YYYY'

it might be helpful for someone it gave me really hard time :)

Solution 3

The new version has changed.. for the latest version use the code below:

$('#UpToDate').datetimepicker({
        format:'MMMM DD, YYYY',
        maxDate:moment(),
        defaultDate:moment()            
    }).on('dp.change',function(e){
        console.log(e);
    });

Solution 4

Try this:

$(".datepicker").on("dp.change", function(e) {
    alert('hey');
});

Solution 5

I found a short solution for it. No extra code is needed just trigger the changeDate event. E.g. $('.datepicker').datepicker().trigger('changeDate');

Share:
133,480
Marijus
Author by

Marijus

N/A

Updated on July 26, 2022

Comments

  • Marijus
    Marijus almost 2 years
    <script type="text/javascript">
        // When the document is ready
        $(document).ready(function () {
            $('.datepicker').datepicker({
                format: "yyyy-mm-dd",
            }).on('changeDate', function(ev){
                // do what you want here
                $(this).datepicker('hide');
            }).on('changeDate', function(ev){
                if ($('#startdate').val() != '' && $('#enddate').val() != ''){
                    $('#period').text(diffInDays() + ' d.'); 
                } else {
                    $('#period').text("-");
                }
            });
        });
    </script>
    

    So here's what my datepicker looks like. So basically when I change date by clicking mouse it works good, however when I manually change date with keyboard or manually clear the date change date event doesn't get called. Is this a bug or am I doing something wrong here ?