7

I am using a date picker from this source http://jqueryui.com/datepicker/#buttonbar , I am trying to let "Today" button on the button bar to be active, can any one help me please.

$(".datepicker").datepicker({
            showButtonPanel: true, closeText: 'Clear',
             gotoCurrent : true,
            changeMonth: true,
            changeYear: true,
            yearRange: '1900, 2300',
            dateFormat: _DateFormatDatePicker,             
            onSelect: function (dateText, inst) {
                dateAsString = dateText; //the first parameter of this function
                var dateAsObject = $(this).datepicker('getDate'); //the getDate method
                document.getElementById('<%=hdnTempDate.ClientID%>').value = dateText;
            }
Alaa
  • 207
  • 2
  • 7
  • 19

4 Answers4

16

Try this:

$(".datepicker").datepicker({
    showOn: "button",
    showButtonPanel: true,
    buttonImage: buttonCalendar,
    buttonImageOnly: true,
    buttonText: ""
});

and call this js code in the pages where you have the calendar.

$.datepicker._gotoToday = function(id) { 
    $(id).datepicker('setDate', new Date()).datepicker('hide').blur(); 
};
periback2
  • 1,459
  • 4
  • 19
  • 36
  • Thanks for the `$.datepicker._gotoToday = function(id) { $(id).datepicker('setDate', new Date()).datepicker('hide').blur(); };` – Linga Aug 19 '16 at 10:54
  • A bit more correct is `$(id).datepicker('setDate', new Date()).datepicker('hide').blur().change();` – Kotodid Nov 15 '17 at 15:14
10

Try this

$(function() {
    $( "#datepicker" ).datepicker({
        showButtonPanel: true
    });
});
Linga
  • 10,379
  • 10
  • 52
  • 104
2

Enable the Buttons panel by: showButtonPanel: true. Then place the following code after your datepicker JS code:

var _gotoToday = jQuery.datepicker._gotoToday;
jQuery.datepicker._gotoToday = function(a){
    var target = jQuery(a);
    var inst = this._getInst(target[0]);
    _gotoToday.call(this, a);
    jQuery.datepicker._selectDate(a, jQuery.datepicker._formatDate(inst,inst.selectedDay, inst.selectedMonth, inst.selectedYear));
};
helencrump
  • 1,351
  • 1
  • 18
  • 27
Tahir
  • 483
  • 1
  • 4
  • 17
0

Insert this code instead.

todayBtn: "linked",

And a bonus code that clear the input field

clearBtn: true,

Final code:

$(".datepicker").datepicker({
  showButtonPanel: true,
  closeText: 'Clear',
  todayBtn: "linked",
  clearBtn: true,
  gotoCurrent : true,
  changeMonth: true,
  changeYear: true,
  yearRange: '1900, 2300',
  dateFormat: _DateFormatDatePicker,             
  onSelect: function (dateText, inst) {
    dateAsString = dateText; //the first parameter of this function
    var dateAsObject = $(this).datepicker('getDate'); //the getDate method
    document.getElementById('<%=hdnTempDate.ClientID%>').value = dateText;
  }