Mohammad
Mohammad

Reputation: 3547

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!

Upvotes: 1

Views: 4972

Answers (1)

clyz
clyz

Reputation: 165

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');
});

Upvotes: 1

Related Questions