Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

v-model won't detect changes made by jQuery trigger event

With Vue.js I'm using jQuery Tempus Dominus datepicker.

<input type="text"
       class="form-control datetimepicker-input"
       id="confirmedDueDate"
       data-target="#confirmedDueDate"
       @focus="openDatetimePicker($event)"  //to show the datetimepicker
       @blur="closeDateTimePicker($event)"  //to close it
       v-model="taskSettings.confirmedDueDate"
       />

I'm facing the following issue: v-model won't detect changes made by the datetimepicker.

I thought I could trigger an event when closing the picker with :

$('#confirmedDueDate').trigger('change');
//or
$('#confirmedDueDate').trigger('input');

but with this is not working.

Is there a known workaround for such cases?

like image 813
Sebastien D Avatar asked Mar 13 '18 16:03

Sebastien D


People also ask

Does jQuery Val trigger change event?

When you dynamically set a value in a textfield using jQuery . val(), you have to manually trigger the . change event if you want to add extra code that trigger when the value of the field change.

How does jQuery trigger work?

jQuery trigger() Method The trigger() method triggers the specified event and the default behavior of an event (like form submission) for the selected elements. This method is similar to the triggerHandler() method, except that triggerHandler() does not trigger the default behavior of the event.


1 Answers

The command

$('#confirmedDueDate').trigger('input');

Triggers a jQuery.Event Object which Vue will not recognize, since it only knows native DOM events.

You can trigger "manually" an event that Vue will respond to using:

$('#confirmedDueDate')[0].dispatchEvent(new CustomEvent('input'));

And Vue will recognize it as a regular native input evnet.

like image 145
acdcjunior Avatar answered Sep 23 '22 15:09

acdcjunior