title: Getting selected string from autocompleted input box
body: <p>I'm building a form where the first field (text input) is auto-completed from a MySQL table, and the second one (a dropdown) is populated as a result of the choice made in the first one. The autocomplete works fine, but I can't figure out how to get the chosen option from it; instead, I'm only getting the search string that I actually typed (e.g., 'eggn' instead of the 'Eggnog' option that I picked); in fact, the 'onchange' function fires off before the selected option even gets copied to the input box.</p>
  
  <p>Really would appreciate some help with this one.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      $(document).ready(function () {
          $("#name").autocomplete({
              source: 'getautocomplete.php',
              minLength: 2
          });
      });
  
      function ajaxfunction(key) {
          alert(key);
          $.ajax({
              url: 'get_units1.php?key=' + key,
              success: function (data) {
                  $("#sub").html(data);
              }
          });
      }
  &lt;/script&gt;
  
  &lt;form method="post"&gt;
      &lt;input type='text' id='name' onchange="ajaxfunction(this.value)"&gt;
      &lt;select id="sub"&gt;
          &lt;option value=""&gt;Choose the amount&lt;/option&gt;
      &lt;/select&gt;
      &lt;input type='submit'&gt;
      &lt;input type='reset'&gt;
  &lt;/form&gt;</code></pre>
  </div>
  </div>
  </p>