Skip to content
Advertisement

Select2 on change event is not working in Vuejs

I was working with select2 in vuejs , I found vuejs is not working with jquery select2 as vuejs is working with navite html.

I am using this code

JavaScript
JavaScript

Please share your reply to handle this problem.

Advertisement

Answer

To get this to work with a directive, we need to understand how v-model works. From the docs:

JavaScript

is just syntactic sugar for:

JavaScript

In the case of a select element, v-model will listen for the change event (not input). So, if the directive dispatches a change event when the element changes, then v-model will work as expected.

Here is an updated version of your code (works in Vue 2):

JavaScript
JavaScript

And here’s a version that works in Vue 3 (custom directives have different syntax, linked here):

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement