103

How do I hide the calendar after a date is selected? Is there a specific function that I can use? My code below:

$('#dp1').datepicker({
    format: 'mm-dd-yyyy',
    startDate: '-15d',
    autoclose: true,
    endDate: '+0d' // there's no convenient "right now" notation yet
});

Any help would be appreciated.

93196.93
  • 2,601
  • 1
  • 19
  • 17
jheul
  • 1,201
  • 3
  • 9
  • 15

18 Answers18

166

You can use event changedate() to keep track of when the date is changed together with datepicker('hide') method to hide the datepicker after making selection:

$('yourpickerid').on('changeDate', function(ev){
    $(this).datepicker('hide');
});

Demo

UPDATE

This was the bug with autoclose: true. This bug was fixed in latest master. SEE THE COMMIT. Get the latest code from GitHub

CJ Ramki
  • 2,620
  • 3
  • 25
  • 47
Felix
  • 37,892
  • 8
  • 43
  • 55
  • 7
    I would recommend additionally checking if current view mode is 'days' : `if (ev.viewMode === 'days') {$(this).datepicker('hide');}` as you probably don't want to hide datepicker after selecting a month or a year} – turan May 07 '14 at 16:57
  • 1
    It's looks like autoclose is set to false by default... `$(".date").datepicker({... autoclose: true, ... });` works nice !!! – Dani Feb 27 '19 at 08:44
49

If it's any help to anyone, the Version 2.0 of the bootstrap datepicker no longer works with the accepted answer.

Here's how I got it working on mine:

$('yourpickerid').datepicker({
    format: 'dd/mm/yyyy',
}).on('changeDate', function(e){
    $(this).datepicker('hide');
});

See http://bootstrap-datepicker.readthedocs.org/en/latest/events.html#changedate

Ola
  • 1,033
  • 9
  • 6
  • There's a version 2? I see v1.6.4 as the latest release on Github – garryp Oct 25 '16 at 12:21
  • Use autoclose (see @Salim's answer or the updated accepted answer) instead of handling the event manually. If you hide the datepicker when the date is changed, then attempting to type in the date can cause unwanted behavior. – Ross Brasseaux Aug 17 '17 at 17:43
43
$('#input').datepicker({autoclose:true});
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
6
  1. Simply open the bootstrap-datepicker.js
  2. find : var defaults = $.fn.datepicker.defaults
  3. set autoclose: true

Save and refresh your project and this should do.

Vivek Kumar
  • 2,625
  • 2
  • 25
  • 33
user3615318
  • 125
  • 1
  • 1
4

If you're looking to override the behavior of the calendar in general, globally, try editing the Datepicker function (in my example it was line 82),

from

    this.autoclose = false;

to

    this.autoclose = true;

Worked fine for me, as I wanted to have all my calendar instances behave the same.

  • 2
    No need to change the default properties in the js library. You can pass it as a property of the object to the function when you call it. Example: $('#dob').datepicker({format: 'dd/mm/yyyy', autoclose:true}); – Zeeshan Dec 18 '15 at 05:27
4

The problem can be stopped, blocking hide event for input element by this linese:

var your_options = { ... };
$('.datetimepicker').datetimepicker(your_options).on('hide', function (e) {
    e.preventDefault();
    e.stopPropagation();
});
Amaan Iqbal
  • 761
  • 2
  • 9
  • 25
papacho
  • 41
  • 1
  • Thanks mate ! i was getting mad because the input got hidden after date selection, and this solved my problem. Cheers ! – spacebiker Jan 03 '17 at 02:00
2

Close datetimepicker when date select(datetimepicker show date with time)

$('.datepicker').datepicker({
    autoclose: true,
    closeOnDateSelect: true
}); 
maazza
  • 7,016
  • 15
  • 63
  • 96
Gosha
  • 21
  • 1
2

In bootstrap 4 use "autoHide : true"

$('#datepicker1').datepicker({
    autoHide: true,
    format: 'mm-yyyy',
    endDate: new Date()
});
Ankit24007
  • 848
  • 9
  • 19
1
$('yourpickerid').datetimepicker({
           pickTime: false
}).on('changeDate', function (e) {
           $(this).datetimepicker('hide');
});
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
mirmo
  • 53
  • 10
1

Having problem with clock still showing even if I i wrote format: 'YYYY-MM-DD',

I hade to set pickTime: false and after change->hide I hade to focus->show

$('#VBS_RequiredDeliveryDate').datetimepicker({
  format: 'YYYY-MM-DD',
  pickTime: false
});

$('#VBS_RequiredDeliveryDate').on('change', function(){
    $('.datepicker').hide();
});

$('#VBS_RequiredDeliveryDate').on('focus', function(){
    $('.datepicker').show();
});
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
dyrwoolf
  • 11
  • 1
0

You can change source code, bootstrap-datepicker.js. Add this.hide(); like ne

  if (this.viewMode !== 0) {
    this.date = new Date(this.viewDate);
    this.element.trigger({
        type: 'changeDate',
        date: this.date,
        viewMode: DPGlobal.modes[this.viewMode].clsName
    });
    this.hide();//here
 }
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
0

I got a perfect solution:

$('#Date_of_Birth').datepicker().on('changeDate', function (e) {
    if(e.viewMode === 'days')
        $(this).blur();
});
Gordon Ma
  • 239
  • 2
  • 6
0
$('.datepicker').datepicker({
    autoclose: true
}); 
devpro
  • 16,184
  • 3
  • 27
  • 38
user3706926
  • 181
  • 2
  • 12
  • Another answer on this question is exactly the same as yours. OP also has this `autoclose: true` in their example code. – 93196.93 Jan 15 '16 at 20:42
0

I changed to datetimepicker and format to 'DD/MM/YYYY'

$("id").datetimepicker({
    format: 'DD/MM/YYYY',
}).on('changeDate', function() {
    $('.datepicker').hide();
});
Amaan Iqbal
  • 761
  • 2
  • 9
  • 25
Sorter
  • 9,704
  • 6
  • 64
  • 74
0

For datetime picker

$('yourpickerid').datetimepicker({
        format: 'dd/mm/yyyy',
}).on('changeDate', function(e){
        $(this).datetimepicker('hide');
});
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
srinivas gowda
  • 486
  • 5
  • 23
0

At least in version 2.2.3 that I'm using, you must use autoClose instead of autoclose. Letter case matters.

ktamlyn
  • 4,519
  • 2
  • 30
  • 41
TimA
  • 1
  • 1
0

Use this for datetimepicker, it works fine

$('#Date').data("DateTimePicker").hide();
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
sugumar
  • 1
  • 1
0

Haven't seen this mentioned, but this is what fixed it for me:

switchOnClick: true
edrakali
  • 155
  • 1
  • 5