Dynamically adding references to js scripts in the header?

0 like 0 dislike
10 views
Hi all, help pls. I get this error : Uncaught TypeError: $(...).suggestions is not a function
The task is to dynamically load jQuery plugin from the service https://dadata.ru/

$("head").append('');$("head").append('');$("#name").suggestions({token: "bdb95d17661d53fedb611214bb3970fdad73a45a",type: "NAME",count: 5,/* Вызывается, когда пользователь выбирает одну из подсказок */onSelect: function(suggestion) {console.log(suggestion);}});


Here's how it works:
<!--[if lt IE 10]><![endif]-->$("#address").suggestions({token: "bdb95d17661d53fedb611214bb3970fdad73a45a",type: "ADDRESS",count: 5,/* Вызывается, когда пользователь выбирает одну из подсказок */onSelect: function(suggestion) {console.log(suggestion);}});
by | 10 views

2 Answers

0 like 0 dislike
You use suggestions before it was uploaded, so I get "suggestions is not a function"
by
0 like 0 dislike
Enough native component to the document to add any head.
In General, check the scripts counters Google or Yandex metrics, there is a more compact tricks with the modification of the head.
(function() { var src = 'https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/css/suggestions.min.css'; var link = document.createElement('link'); link.setAttribute('src', src); link.setAttribute('type', 'text/css'); link.setAttribute('rel', 'stylesheet'); document.head.appendChild(link); src = 'https://cdn.jsdelivr.net/npm/suggestions-jquery@17.12.0/dist/js/jquery.suggestions.min.js'; var scrt = document.createElement('script'); scrt.setAttribute('src', src); document.head.appendChild(scrt); } )();
by

Related questions

0 like 0 dislike
6 answers
0 like 0 dislike
2 answers
0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
0 like 0 dislike
3 answers
asked May 11, 2019 by potapchino
110,608 questions
257,186 answers
0 comments
33,652 users