Home » Jquery » Trigger function when date is selected with jQuery UI datepicker

Trigger function when date is selected with jQuery UI datepicker

Posted by: admin November 30, 2017 Leave a comment

Questions:

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

Answers:

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

    }
});

Questions:
Answers:

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 🙂

Questions:
Answers:

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

Questions:
Answers:
$(".datepicker").datepicker().on("changeDate", function(e) {
   console.log("Date changed: ", e.date);
});

Questions:
Answers:

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