NeedACar NeedACar - 1 year ago 291
Javascript Question

bootstrap-datepicker empties field after selecting current date

If I have autoclose enabled, and I select the field from the calendar that is already chosen, it empties the field and then closes the datepicker as expected. How can I still have the autoclose feature, but not have it empty the field?

Look at the demo for an example,

  1. Make sure autoclose is on

  2. Select a date.

  3. open the datepicker again, select the currently selected date again.

  4. field is empty again


Answer Source

Bootstrap Datepicker provides events that you can leverage to accomplish your goal.

Here's one way to do it:

$('#sandbox-container input').datepicker({
    autoclose: true

$('#sandbox-container input').on('show', function(e){
    if ( ) {
    else {
         $(this).data('stickyDate', null);

$('#sandbox-container input').on('hide', function(e){
    var stickyDate = $(this).data('stickyDate');

    if ( ! && stickyDate ) {
        $(this).datepicker('setDate', stickyDate);
        $(this).data('stickyDate', null);

Not necessarily the most elegant, but as you can see here, it does the trick: (tested in Chrome)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download