1

I am trying to use this jQuery plugin in my Vue.js single file component.

as required in the linked autocomplete plugin I imported references in index.html, for them to be available all other vue components where I need to use autocomplete feature in input field .

  <script src="//code.jquery.com/jquery-1.12.4.js"></script>
        <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/smoothness/jquery-ui.css">
        <script src="//code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

But when I include below code in the script tag of my single file component.

 $("#autocomplete").autocomplete({
        source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]
    });

I get error console saying:

> TypeError: $("#autocomplete").autocomplete is not a function. (In
> '$("#autocomplete").autocomplete({
>     source: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"] })', '$("#autocomplete").autocomplete' is undefined)

But I have included the dependencies in index.html

3 Answers 3

0

Solution: use a component as a wrapper

Check out the tutorial here: https://vuejsdevelopers.com/2017/05/20/vue-js-safely-jquery-plugin/

Sign up to request clarification or add additional context in comments.

3 Comments

;( the first line says : > It's not a great idea to use jQuery and Vue.js in the same UI. Don't do it if you can avoid it. secondly I get error at the time of instantiation
Yeah i don't recommend using jquery in Vue.js either, if you just want a autocomplete feature there are plenty of components out there. For example this npmjs.com/package/vue2-autocomplete-js
I have tried in the first attempt and that didn't worked at all, I also tried vie-single-select that had problem you cannot enter new item not in list
0

Check the order of js files/script inside your html. It's possible that the Vue script is executed before jQuery is loaded. Move jQuery-related script tags above Vue script tags. You can also safely check if jQuery is available in your Vue script by checking for existence of window.$:

if (typeof window.$ === 'function') {
  $("#autocomplete").autocomplete({
        source: [ "c++", "java", "php", "coldfusion", "javascript", "asp", "ruby" ]
    });
}

6 Comments

well if it was the case of jQuery not loading up then I might have go terror at $("#autocomplete") but I am getting error .autocomplete
@Ciastopiekarz Yes, my mistake not noticing that. However, I cannot recreate your issue in CodeSandbox codesandbox.io/s/74wy254mx0. If you can provide more detail of how you implement your Vue component with jQuery UI I might find something.
thank you @blaz that would be great if you can please have a look I have uploaded my project to codebase
I was using eonasdan-bootstrap-datetimepicker component that was causing the problems so I got rid of it and used vue-datetime component then after 4 days of hard work, I finally succeeded adding jQuery based autocomplete working perfectly but my form had a second input field that required same feature and i realised even if we have different id setup for two different input field it resets the old one. so I can only use 1 input field with feature of autocomplete.
@Ciastopiekarz sorry I cannot figure out what is wrong with your code. Even in main.js, autocomplete is not defined while in my demo it is there.
|
-1

I would suggest u not to use Jquery in Vue. Its bad practice. There is a number of vue plugins u can use and even create your own (which is best option IMHO)

https://www.npmjs.com/package/vue2-autocomplete-js https://github.com/paliari/v-autocomplete https://vuejsexamples.com/tag/autocomplete/ https://alligator.io/vuejs/vue-autocomplete-component/

1 Comment

I agree with you , but right now I am short of time, and yesterday night I stayed up late to get things working using jQuery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.