5

I need to be able to grab the selected date from the Datepicker and populate another textbox with the selected value. I am currently using the onClose function like this:

$('#defaultDate').datepicker({
    onClose:function(theDate) {
        $('#txtEntry1').text = theDate;
    }
});
kapa
  • 77,694
  • 21
  • 158
  • 175
Matt
  • 5,542
  • 14
  • 48
  • 59

2 Answers2

12

There's an easier way to do it. Use the built-in alt-field option and you're done.

Populate an alternate field with its own date format whenever a date is selected using the altField and altFormat options. This feature could be used to present a human-friendly date for user selection, while passing a more computer-friendly date through for further processing.

kapa
  • 77,694
  • 21
  • 158
  • 175
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • Please note: the alt-field option doesn't work for spans (probably other html elements as well) – Nathan Koop May 25 '12 at 16:28
  • @NathanKoop well... _yeah_, a `` is not a form field. – Matt Ball May 25 '12 at 16:42
  • I didn't notice any qualifiers (I guess "field" should be somewhat of a clue) I just wanted to populate a span elsewhere on the form based on this input. Making a readonly textbox isn't really a great solution in this case. – Nathan Koop May 25 '12 at 16:46
6

Have you looked through the docs? http://docs.jquery.com/UI/Datepicker#events

onSelect function(dateText, inst)

Allows you to define your own event when the datepicker is selected. The function receives the selected date as text and the datepicker instance as parameters. this refers to the associated input field. Code examples

Supply a callback function to handle the onSelect event as an init option.

$('.selector').datepicker({
   onSelect: function(dateText, inst) { ... }
});
Thomas Ahle
  • 30,774
  • 21
  • 92
  • 114