Search code examples
jqueryuidatepicker

jQuery Datepicker - close on input click


I am using jQuery Datepicker and I have a little problem.

If the datepicker is opened, and you click on the input field again, nothing happens. How can I change that.. the picker to be closed on input click if it is already opened ?

Thanks in advance...


Solution

  • Since it's bound to focus (by default), you can just bind your own .mousedown() handler and it won't interfere, like this:

    $("#datepicker").datepicker();
    $("#datepicker").mousedown(function() {
        $(this).datepicker("hide");    
    });
    

    You can give it a try here. I'm using mousedown because that's how it's close behavior is detected as well, so just being consistent to more future-proof this behavior.