Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I clear/reset the selected dates on the jQuery UI Datepicker calendar?

How do I reset the datepicker calendar values?.. The min and max date restrictions?

The problem is that when I clear the dates (by deleting the textbox values), the previous date restrictions are still applied.

I've been sorting through the documentation and nothing jumped out as a solution. I also wasn't able to find a quick fix on a SO/google search

http://jsfiddle.net/CoryDanielson/tF5MH/


Solution:

// from & to input textboxes with datepicker enabled
var dates = $("input[id$='dpFrom'], input[id$='dpTo']");

// #clearDates is a button to clear the datepickers
$('#clearDates').on('click', function(){
    dates.attr('value', '');
    dates.each(function(){
        $.datepicker._clearDate(this);
    });
});​​

_.clearDate() is a private method of the DatePicker object. You won't find it in the public API on jQuery UI's website, but it works like a charm.

like image 645
Cory Danielson Avatar asked Feb 24 '12 17:02

Cory Danielson


People also ask

How do you clean a date picker?

Once click the reset button, the DatePicker would return blank value. You can find it from File – App Setting – Advanced Setting – Formula-level error management. Hi @Anonymous , You have to add a Reset button in the DatePicker DateCard, if you want to only reset the datepicker control instead of the whole form.

How can change date format in jQuery UI datepicker?

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


9 Answers

I was trying to accomplish this very thing, that is, empty the datepicker so that filters entered by the user could be removed. Googling a bit I found this sweet piece of code:

You can add the clear feature even on read only fields. Just add the following code to your datepicker:

}).keyup(function(e) {
    if(e.keyCode == 8 || e.keyCode == 46) {
        $.datepicker._clearDate(this);
    }
});

People will be able to highlight (even Read Only fields) and then use backspace or delete key to remove the date using _clearDate function.

Source

like image 172
Leniel Maccaferri Avatar answered Oct 15 '22 19:10

Leniel Maccaferri


Did you try:

$('selector').datepicker('setDate', null);

That should work according to the API documentation

like image 41
DavidWainwright Avatar answered Oct 15 '22 20:10

DavidWainwright


you just need to set the options again to null:

dates.datepicker( "option" , {
    minDate: null,
    maxDate: null} );

I've changed your jsfiddle: http://jsfiddle.net/tF5MH/9/

like image 24
Simon Wang Avatar answered Oct 15 '22 21:10

Simon Wang


$('.date').datepicker('setDate', null);
like image 41
Soni Sharma Avatar answered Oct 15 '22 19:10

Soni Sharma


Try changing the clearing code to:

$('#clearDates').on('click', function(){
    dates.attr('value', '');
    $("input[id$='dpFrom'], input[id$='dpTo']").datepicker( "option", "maxDate", null );
    $("input[id$='dpFrom'], input[id$='dpTo']").datepicker( "option", "minDate", null );
});
like image 27
j08691 Avatar answered Oct 15 '22 19:10

j08691


Try This, This Will Add Clear Button On Jquery Calender That Will Clear Date.

$("#DateField").datepicker({
    showButtonPanel: true,
    closeText: 'Clear',
         onClose: function (dateText, inst) {
        if ($(window.event.srcElement).hasClass('ui-datepicker-close'))
        {
            document.getElementById(this.id).value = '';
        }
    }
});
like image 38
sohaib Avatar answered Oct 15 '22 21:10

sohaib


Reset the max date attributes on your datepicker when you click clear.

From jquery website

Get or set the maxDate option, after init.

    //getter
    var maxDate = $( ".selector" ).datepicker( "option", "maxDate" );
    //setter
    $( ".selector" ).datepicker( "option", "maxDate", '+1m +1w' );
like image 40
Mark W Avatar answered Oct 15 '22 20:10

Mark W


The obvious answer was the one that worked for me.

$('#datepicker').val('');

where $('#datepicker') is the id of the input element.

like image 42
omarjebari Avatar answered Oct 15 '22 21:10

omarjebari


I know it's a bit too late, but here's a simple peace of code that did it for me:

$('#datepicker-input').val('').datepicker("refresh");
like image 43
Miguel BinPar Avatar answered Oct 15 '22 19:10

Miguel BinPar