Alaa
Alaa

Reputation: 217

Turn on Today button on date picker

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;
            }

Upvotes: 8

Views: 39840

Answers (4)

wakawakapogsSr
wakawakapogsSr

Reputation: 113

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;
  }

Upvotes: 0

Try this

$(function() {
    $( "#datepicker" ).datepicker({
        showButtonPanel: true
    });
});

Upvotes: 11

Tahir
Tahir

Reputation: 511

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));
};

Upvotes: 2

periback2
periback2

Reputation: 1469

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(); 
};

Upvotes: 16

Related Questions