63

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

0

5 Answers 5

104

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

    }
});
3
13

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 :-)

1
  • @inkwai If my anwser helped please accept it so others can learn form it. If not how can i help you further?
    – RTB
    Commented Jul 23, 2012 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             
      });
5
$(".datepicker").datepicker().on("changeDate", function(e) {
   console.log("Date changed: ", e.date);
});
2
  • thanks, straightforward solution, without those val() funny stuff, also directly can be addressed this way ` $(".datepicker[name=datepicker2]").datepicker('getDate');`
    – FantomX1
    Commented Sep 3, 2019 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 stackoverflow.com/a/17288563/3419535 , first = $(".datepicker[name=datepicker1]").datepicker('getDate');
    – FantomX1
    Commented Sep 4, 2019 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
            }
        });
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.