Alex Rivera | Logout

How do you add buttons to a jQuery datepicker in the button panel?

Asked 2011-01-04T21:53:56.147
24

Do I just append the element to the panel wrapper and call .button()?

Edit
Report

2 Answers

46

If you want to keep the "clear" button while scrolling through the months, use onChangeMonthYear:.

An example:

$( "#datepicker" ).datepicker({
    showButtonPanel: true,
    beforeShow: function( input ) {
        setTimeout(function() {
            var buttonPane = $( input )
                .datepicker( "widget" )
                .find( ".ui-datepicker-buttonpane" );

            $( "<button>", {
                text: "Clear",
                click: function() {
                //Code to clear your date field (text box, read only field etc.) I had to remove the line below and add custom code here
                    $.datepicker._clearDate( input );
                }
            }).appendTo( buttonPane ).addClass("ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all");
        }, 1 );
    },
    onChangeMonthYear: function( year, month, instance ) {
        setTimeout(function() {
            var buttonPane = $( instance )
                .datepicker( "widget" )
                .find( ".ui-datepicker-buttonpane" );

            $( "<button>", {
                text: "Clear",
                click: function() {
                //Code to clear your date field (text box, read only field etc.) I had to remove the line below and add custom code here
                    $.datepicker._clearDate( instance.input );
                }
            }).appendTo( buttonPane ).addClass("ui-datepicker-clear ui-state-default ui-priority-primary ui-corner-all");
        }, 1 );
    }
});
answered 2011-11-10T05:38:08.780
2

If you are talking about using button/icon for triggering you can use this:

<script>
$(function() {
    $( "#datepicker" ).datepicker({
        showOn: "button",
        buttonImage: "images/calendar.gif",
        buttonImageOnly: true
    });
});
</script>

If you want to assign it to a link/button:

<script>
$(function() {    
       $(button_id).click(function() {
          $(input).datepicker('show');
       });
    });
</script>
answered 2011-01-04T21:58:25.940

Your Answer