62

How can I trigger onSelect event when I select a selected date on jQuery UI datepicker?

TheAlbear
  • 5,507
  • 8
  • 51
  • 84
inkwai
  • 631
  • 1
  • 5
  • 5

5 Answers5

105

Working demo : http://jsfiddle.net/YbLnj/

Documentation: http://jqueryui.com/demos/datepicker/

code

$("#dt").datepicker({
    onSelect: function(dateText, inst) {
        var date = $(this).val();
        var time = $('#time').val();
        alert('on select triggered');
        $("#start").val(date + time.toString(' HH:mm').toString());

    }
});
Zanon
  • 29,231
  • 20
  • 113
  • 126
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
12

Use the following code:

$(document).ready(function() {
    $('.date-pick').datepicker( {
     onSelect: function(date) {
        alert(date)
     },
    selectWeek: true,
    inline: true,
    startDate: '01/01/2000',
    firstDay: 1,
  });
});

You can adjust the parameters yourself :-)

RTB
  • 5,773
  • 7
  • 33
  • 50
  • @inkwai If my anwser helped please accept it so others can learn form it. If not how can i help you further? – RTB Jul 23 '12 at 13:43
7

If you are also interested in the case where the user closes the date selection dialog without selecting a date (in my case choosing no date also has meaning) you can bind to the onClose event:

$('#datePickerElement').datepicker({
         onClose: function (dateText, inst) {
            //you will get here once the user is done "choosing" - in the dateText you will have 
            //the new date or "" if no date has been selected             
      });
Ando
  • 11,199
  • 2
  • 30
  • 46
5
$(".datepicker").datepicker().on("changeDate", function(e) {
   console.log("Date changed: ", e.date);
});
Serdar KUŞ
  • 404
  • 1
  • 4
  • 16
  • thanks, straightforward solution, without those val() funny stuff, also directly can be addressed this way ` $(".datepicker[name=datepicker2]").datepicker('getDate');` – FantomX1 Sep 03 '19 at 15:24
  • however when using functions like getDate when inside your handler, it fails when it tries to retrieve the status of datePicker object, like this https://stackoverflow.com/a/17288563/3419535 , `first = $(".datepicker[name=datepicker1]").datepicker('getDate');` – FantomX1 Sep 04 '19 at 21:29
0

If the datepicker is in a row of a grid, try something like

editoptions : {
    dataInit : function (e) {
        $(e).datepicker({
            onSelect : function (ev) {
                // here your code
            }
        });
    }
}
Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Fseee
  • 2,476
  • 9
  • 40
  • 63