Search code examples
jquerytwitter-bootstrapdatepickerbootstrap-datepicker

bootstrap datepicker: run function when date is changed


I want to run a function when the user changes the date input value using the datepicker, here is my code:

The datepicker used.

html:

<div class="input-group date form-group" data-provide="datepicker">
    <input type="text" class="form-control" id="birthdate" 
            name="birthdate" placeholder="Student Birth Date...">
    <div class="input-group-addon">
        <span class="glyphicon glyphicon-th"></span>
    </div>
</div>

js:

$('#birthdate').datepicker({
    format: 'yyyy/mm/dd',
    startDate: '-3d'
});

$('#birthdate').datepicker()
    .on('changeDate', function(e) {
        alert();
});

When the date is changed, nothing happened. I've searched for this question before asking it, but no answer solved my problem!


Solution

  • Try with 'change event'

    Demo: http://jsfiddle.net/Z3CRA/99/

    $('#birthdate').datepicker({
        format: 'yyyy/mm/dd',
        startDate: '-3d'
    });
    
    
    $( "#birthdate" ).change(function() {
      alert( "Handler for .change() called." );
    });
    

    UPDATE: You should select div#datepicker not direct the input.

    Demo: http://jsfiddle.net/8ac7b7Lh/4/

    $('#datepicker').datepicker({
        format: 'yyyy/mm/dd',
        startDate: '-3d'
    });
    
    $('#datepicker').datepicker().on('changeDate', function (ev) {
      console.log('Input changed');
    });