Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

<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 ?

like image 732
Marijus Avatar asked Mar 19 '14 13:03

Marijus


People also ask

How do I change the default date format in bootstrap Datepicker?

In order to set the date format, we only need to add format of one argument and then we will add our required format, which is shown in the following example: Example 1: In this example, we are going to use dd-mm-yyyy format.

How do I know if my Datepicker is loaded?

You can check to see if the datepicker script has loaded using: if ($. fn. datepicker) { // ... }

How do I change the default date in Datepicker to today?

To set current date in control to which jQuery UI datepicker bind, use setDate() method. Pass date object which needs to be set as an argument to setDate() method. If you want to set it to current date then you can pass 'today' as argument.


1 Answers

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("-");     } } 
like image 65
Bradley Trager Avatar answered Sep 30 '22 04:09

Bradley Trager