autocomplete works only sometimes

autocomplete works only sometimes

First jquery project for me and first time doing anything with JS in a long time, so be gentle. :)

So first let me tell you what I am trying to accomplish. I have a radio button, and depending on what value is selected I want to change the autocomplete source for a text box. So if the user selects option 1 on the radio button then get autocomplete list 1 and if they select option 2 on the radio button they should get autocomplete list 2.

If I assign an autocomplete at the top level:

  1. $('#search-input').autocomplete({
  2.     source: function (request, response) {
  3.         var SearchType = $("input[name='SearchType']").val();
  4.         console.log(SearchType);
  5.         $.getJSON("https://rxnav.nlm.nih.gov/REST/spellingsuggestions.json?name=" + request.term, function (data) {
  6.             response($.map(data.suggestionGroup.suggestionList.suggestion, function (value, key) {
  7.                 return {
  8.                     label: value,
  9.                     value: value
  10.                 };
  11.             }));
  12.         });
  13.     },
  14.     minLength: 3,
  15.     delay: 300
  16. });

That works just fine. If I try and define the autocomplete inside the change() of the radio button then I get a typeerror of "undefined is not a function" on line of the code below:

  1. $("input[name='SearchType']").change(function(){
  2.     var placeholder=$(this).val();
  3.     $('#search-input').attr('placeholder',placeholder);
  4.     //only populate drugs
  5.     console.log(placeholder);
  6.     console.log($('#search-input').val());
  7.     if(placeholder == 'Drug'){
  8. $('#search-input').autocomplete({
  9.    source: function (request, response) {
  10. $.getJSON("https://rxnav.nlm.nih.gov/REST/spellingsuggestions.json?name=" + request.term, function (data) {
  11.    response($.map(data.suggestionGroup.suggestionList.suggestion, function (value, key) {
  12. return {
  13.    label: value,
  14.    value: value
  15. };
  16.    }));
  17. });
  18.    },
  19.    minLength: 3,
  20.    delay: 300
  21. });
  22.     }else if (placeholder == 'Event'){
  23. $('#search-input').autocomplete = null;
  24. $('#search-input').autocomplete({
  25.    source: function (request, response) {
  26.        $.post("http://api.ohdsi.org/WebAPI/CS1/vocabulary/search", { 'QUERY':request.term, 'VOCABULARY_ID':'SNOMED','DOMAIN_ID':'Condition'}, function (data) {
  27.    console.log(data);
  28.            response($.map(data, function (value, key) {
  29.                return {
  30.                    label: value,
  31.                    value: value
  32.                };
  33.            }));
  34.        },'json');
  35.    },
  36.    minLength: 3,
  37.    delay: 300
  38. });
  39.     }
  40. });

Most of the issues I've seen with this are with the jquery.js file not being loaded properly, or being loaded twice etc which I know isn't the case here because it works when I just assign the autocomplete() not in another form handle's change() but just at top-level.

Any help appreicated, thanks in advance!