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,8211 gold badge21 silver badges17 bronze badges
asked Jan 16, 2014 at 1:33
1
  • 7
    Why isn't this the default behavior of the library? Commented May 21, 2014 at 18:32

18 Answers 18

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,6323 gold badges27 silver badges47 bronze badges
answered Jan 16, 2014 at 1:41
Sign up to request clarification or add additional context in comments.

2 Comments

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}
It's looks like autoclose is set to false by default... $(".date").datepicker({... autoclose: true, ... }); works nice !!!
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

answered May 13, 2014 at 11:18

2 Comments

There's a version 2? I see v1.6.4 as the latest release on Github
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.
45
$('#input').datepicker({autoclose:true});
answered Nov 16, 2015 at 19:24

Comments

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,9792 gold badges30 silver badges34 bronze badges
answered Aug 21, 2017 at 6:36

Comments

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.

answered Oct 28, 2014 at 21:45

1 Comment

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});
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
7592 gold badges9 silver badges25 bronze badges
answered Aug 7, 2016 at 18:54

1 Comment

Thanks mate ! i was getting mad because the input got hidden after date selection, and this solved my problem. Cheers !
2

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

$('.datepicker').datepicker({
 autoclose: true,
 closeOnDateSelect: true
}); 
maazza
7,26116 gold badges67 silver badges98 bronze badges
answered Feb 15, 2017 at 12:25

Comments

2

In bootstrap 4 use "autoHide : true"

$('#datepicker1').datepicker({
 autoHide: true,
 format: 'mm-yyyy',
 endDate: new Date()
});
answered Apr 8, 2020 at 9:53

1 Comment

Nice answer Ankit
1

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

ktamlyn
4,7092 gold badges32 silver badges42 bronze badges
answered Sep 1, 2017 at 16:21

Comments

1
$('yourpickerid').datetimepicker({
 pickTime: false
}).on('changeDate', function (e) {
 $(this).datetimepicker('hide');
});
Jignesh Joisar
15.4k6 gold badges69 silver badges61 bronze badges
answered Oct 1, 2014 at 1:08

Comments

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
15.4k6 gold badges69 silver badges61 bronze badges
answered Apr 12, 2016 at 7:51

Comments

0

I got a perfect solution:

$('#Date_of_Birth').datepicker().on('changeDate', function (e) {
 if(e.viewMode === 'days')
 $(this).blur();
});
answered Jun 9, 2015 at 18:57

Comments

0
$('.datepicker').datepicker({
 autoclose: true
}); 
devpro
16.1k3 gold badges29 silver badges39 bronze badges
answered Jan 15, 2016 at 20:18

1 Comment

Another answer on this question is exactly the same as yours. OP also has this autoclose: true in their example code.
0

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

$("id").datetimepicker({
 format: 'DD/MM/YYYY',
}).on('changeDate', function() {
 $('.datepicker').hide();
});
Amaan Iqbal
7592 gold badges9 silver badges25 bronze badges
answered Jun 4, 2016 at 10:44

Comments

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
15.4k6 gold badges69 silver badges61 bronze badges
answered May 20, 2014 at 7:47

Comments

0

For datetime picker

$('yourpickerid').datetimepicker({
 format: 'dd/mm/yyyy',
}).on('changeDate', function(e){
 $(this).datetimepicker('hide');
});
Jignesh Joisar
15.4k6 gold badges69 silver badges61 bronze badges
answered Feb 18, 2017 at 7:17

Comments

0

Use this for datetimepicker, it works fine

$('#Date').data("DateTimePicker").hide();
Jignesh Joisar
15.4k6 gold badges69 silver badges61 bronze badges
answered Nov 24, 2017 at 9:37

Comments

0

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

switchOnClick: true
answered Feb 5, 2020 at 16:59

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.