Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery datePicker date reset

I would like to add a "Reset" control to the datepicker at the bottom of the calendar - where the "close" control goes. This would enable the user to reset the input tied to datepicker to blank (no date).

I can't figure out how to write the bind function, specifically, how do I get a hold of the element bound to the control?

        if (this.displayClear) {
           $pop.append(
              $('<a href="#" id="dp-clear">' + $.dpText.TEXT_CLEAR + '</a>')
                 .bind(
                    'click',
                    function()
                    {
                          c.clearSelected();
                          //help! reset the value to '': $(this.something).val('')
                          c._closeCalendar();
                    }
                 )
           );
        }
like image 991
KateYoak Avatar asked Feb 07 '11 03:02

KateYoak


People also ask

How to clear date in jQuery DatePicker?

To destroy a datepicker in jQuery UI, we will be using destroy() method. jQuery UI destroy() method is used to remove the complete functionality of the datepicker().

How do I add a Clear button in DatePicker?

How to add clear button with DatePicker? Clear button can be included in the DatePicker control. In the create event of DatePicker, clear button element should be appended in the input element and event for clearing the value should bind with the clear button element.

How do you remove a date range picker?

Either DatePicker or DateRangePicker has a cleanable prop which, when set true , displays a "clear" button on the text box that can erase the selected value when clicked on.


3 Answers

Here is working solution, just call cleanDatepicker() before any call to datepicker.

function cleanDatepicker() {
   var old_fn = $.datepicker._updateDatepicker;

   $.datepicker._updateDatepicker = function(inst) {
      old_fn.call(this, inst);

      var buttonPane = $(this).datepicker("widget").find(".ui-datepicker-buttonpane");

      $("<button type='button' class='ui-datepicker-clean ui-state-default ui-priority-primary ui-corner-all'>Delete</button>").appendTo(buttonPane).click(function(ev) {
          $.datepicker._clearDate(inst.input);
      }) ;
   }
}
like image 116
pma_ Avatar answered Oct 26 '22 19:10

pma_


I found a nicer solution:

$(document).ready(function () {
    $(".datepicker").datepicker({
            showOn: 'focus',
            showButtonPanel: true,
            closeText: 'Clear', // Text to show for "close" button
            onClose: function () {
                var event = arguments.callee.caller.caller.arguments[0];
                // If "Clear" gets clicked, then really clear it
                if ($(event.delegateTarget).hasClass('ui-datepicker-close')) {
                    $(this).val('');
                }
            }
    });
});

http://jsfiddle.net/swjw5w7q/1/

like image 24
vellotis Avatar answered Oct 26 '22 18:10

vellotis


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

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

sohaib