3

I have from and to input fields with bootstrap date picker. When query params not empty I would like to change the value of these input fields and trigger the change date event on datepicker because I have a function that calculates total price between dates. When user selects dates from datepicker works fine but if I change the value by jquery it does not calculate.

$('#from').val('<%= @from_date %>').trigger( 'change' );
$('#to').val('<%= @to_date %>').trigger( 'change' );
//function
var dates_avail = new DatesAvailability({
                start: '#from',
                end: '#to'
});

..
W.DatesAvailability = function (opts) {
var options = $.extend({}, defaultOpts, opts);
        var start = options.start + ',' + options.rel_start;
        var end = options.end + ',' + options.rel_end;
        $(start + ',' + end).datepicker({
            autoclose: true,
            format: "yyyy-mm-dd",
            startDate: '+1d',
            endDate: '+3y',
..
}).
on('changeDate', function (e) {
// I would like to trigger this.
..

I also have these lines in bootstrap-datepicker.js

..
if (fromArgs){
                // setting date by clicking
                this.setValue();
                this.element.change();
            }
            else if (this.dates.length){
                // setting date by typing
                if (String(oldDates) !== String(this.dates) && fromArgs) {
                    this._trigger('changeDate');
                    this.element.change();
                }
            }
..
Shalafister's
  • 761
  • 1
  • 7
  • 34

4 Answers4

6

Try using the update mention in the documentation. Something like `$(start + ',' + end).datepicker('update', qParamStart);

If you're trying to figure out how to get the query params this is a good reference.

SimplyComplexable
  • 1,116
  • 11
  • 19
4

You can do this by manually calling the event changeDate.

E.g.

$('.datepicker').datepicker().trigger('changeDate');
Deepak Mahakale
  • 22,834
  • 10
  • 68
  • 88
Musaddiq Khan
  • 1,837
  • 18
  • 16
1

I would do something like this.

$('.datepicker').datepicker('_trigger', 'changeDate');

This uses an undocumented private method, but will fire with event and the extra data you would expect.

barneyp
  • 41
  • 3
0

Inspect the link from the calendar then call it conventionally. For me it was:

$('.ui-state-active').click();

or

$('.ui-datepicker-current-day').click();
estinamir
  • 435
  • 5
  • 11