I found the all the answers a bit not correct to what it should do, at least for me.
The marked correct answer looks like a piece of code that prevents you from tuning again the calendar, after closing, without losing focus from the input, and I'm pretty sure that is not something that should happen.
Here is the solution that worked for me.
html
<p>Date: <input type="text" id="datepicker"></p>
js
var isCalendarOpen = false;
$("#datepicker").datepicker();
$("#datepicker").mousedown(function() {
if(isCalendarOpen){
$(this).datepicker("hide");
isCalendarOpen = false;
return;
}
isCalendarOpen = true;
$(this).datepicker("show");
});
https://jsfiddle.net/Przmak/jt4zgaf6/