Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI: datepicker - How to add close Button (Image!) on the right top?

I want to add a close button at the right top.

I don't want to use the close button at the bottom with the button panel.

How can I do that?

I want something like the "X":

enter image description here

like image 807
Keith L. Avatar asked Apr 23 '12 12:04

Keith L.


People also ask

How to add close button on datepicker?

If you want to specifically add a close button you will have to ". append()" the img/button to the control during runtime and also manipulate the datepicker CSS to cram it into the header.

How can change date format in jQuery ui Datepicker?

inside the jQuery script code just paste the code. $( ". selector" ). datepicker({ dateFormat: 'yy-mm-dd' });

How do I use Daterangepicker?

The datepicker is tied to a standard form input field. Focus on the input (click, or use the tab key) to open an interactive calendar in a small overlay. Choose a date, click elsewhere on the page (blur the input), or hit the Esc key to close. If a date is chosen, feedback is shown as the input's value.

What is Datepicker in jQuery?

Advertisements. Datepickers in jQueryUI allow users to enter dates easily and visually. You can customize the date format and language, restrict the selectable date ranges and add in buttons and other navigation options easily.


3 Answers

You can insert the link in the markup and style it as you wish, attaching an onclick handler to it that will call .datepicker( "hide" ), a built-in method of datepicker:

$( "#datepicker" ).datepicker({
    beforeShow: function( input ) {
        setTimeout(function() {
            var headerPane = $( input )
            .datepicker( "widget" )
            .find( ".ui-datepicker-header" );

            $( "<button>", {
                text: "Close",
                click: function() {
                  $.datepicker.hide();
                }
            }).appendTo( headerPane );
        }, 1 );
    }
});

Also, to make space for the close button, you'll probably need to adjust these styles in order to move the 'next month' button over to the left and make space for 'close' button:

.ui-datepicker .ui-datepicker-next { right:2px; }
.ui-datepicker .ui-datepicker-next-hover { right:1px; }

Note: I've taken the above JS code from this proof of concept and adjusted it accordingly. Hasn't tested it though.

like image 66
montrealist Avatar answered Nov 14 '22 13:11

montrealist


I kept getting this error:

$.datepicker.hide() is not a function

via firebug using @dalbaeb's solution. It was right but I had to tweak it a bit to get it to close the datepicker.

This is how I tweaked it and it worked for me:

$( "#datepicker" ).datepicker({
beforeShow: function( input ) {
    setTimeout(function() {
        var headerPane = $( input )
        .datepicker( "widget" )
        .find( ".ui-datepicker-header" );
        $( "<button>", {
            text: "Close",
            click: function() {

             $('#ui-datepicker-div').hide();         

            }
        }).appendTo( headerPane );
    }, 1 );
}
});

I just replaced:

$.datepicker.hide();

With this:

 $('#ui-datepicker-div').hide(); 

...and it closes the datepicker now! Hope it helps!

like image 20
IchegoFX Avatar answered Nov 14 '22 13:11

IchegoFX


I have done this by styling the Done button

First, enable the showButtonPanel option then change it's text:

$(".calendar").datepicker({
    showButtonPanel: true,
    closeText: '&times;'
});

Then hide the Today button and style the close button:

#ui-datepicker-div .ui-datepicker-current {
    display: none;
}
#ui-datepicker-div .ui-datepicker-close {
    position: absolute;
    top: 0;
    right: 0;
    border: none;
    background: transparent;
    box-shadow: none;
    text-shadow: none;
    font-size: 26px;
    line-height: 1;
    padding: 3px 8px;
    color: #cc6a6a;
    font-weight: 600;
    opacity: 0.8;
}
#ui-datepicker-div .ui-datepicker-close:focus {
    outline: none;
}
#ui-datepicker-div .ui-datepicker-close:hover {
    opacity: 1;
}
like image 35
harisrozak Avatar answered Nov 14 '22 12:11

harisrozak