Search code examples
jqueryjquery-pluginsjquery-ui-datepicker

How to show the datepicker when a button is clicked?


I'm trying to add a custom button to the button panel of jQuery's datepicker. When I apply the following code though nothing happens:

 $('.date-picker').datepicker( {
        changeMonth: true,
        changeYear: true,
        showButtonPanel: true,  
        beforeShow: function(input) {  
            setTimeout(function() {  
            var buttonPane = $(input).datepicker("widget").find( ".ui-datepicker-buttonpane" );  
            var btn = $('<button class="ui-datepicker-current ui-state-default ui-priority-secondary ui-corner-all" type="button">Clear</button>');  
            btn.unbind("click").bind("click", function () {  
                $.datepicker._clearDate( input );  
            });  

            btn.appendTo( buttonPane );  

            }, 1 );  
        } ,
        dateFormat: 'MM yy',
        onClose: function(dateText, inst) { 
            var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
            var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
            $(this).datepicker('setDate', new Date(year, month, 1));
        }
    });

Any help would be appreciated.


Solution

  • $(function(){
        var pickerOpts = {
            showOn: "button"
        };  
        $("#date").datepicker(pickerOpts);
    });