Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
14
rated 0 times [  17] [ 3]  / answers: 1 / hits: 140368  / 10 Years ago, wed, march 19, 2014, 12:00:00
<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 ?


More From » jquery

 Answers
44

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

[#71903] Tuesday, March 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
julieth

Total Points: 382
Total Questions: 99
Total Answers: 85

Location: Cape Verde
Member since Fri, Nov 27, 2020
4 Years ago
;