title: Knockout jQueryUI Autocomplete how to use input value if nothing selected from autocomplete list
body: <p>I am using the custom binding provided in <a href="https://stackoverflow.com/questions/7537002/autocomplete-combobox-with-knockout-js-template-jquery">How to create an auto-complete combobox?</a></p>
  
  <p>I want to allow the user to either select a value from the list of suggestions or enter a value that is not in the list of suggestions.  How can I get the value of the input into my observable field?</p>
  
  <p>For example, if the user types 'smi' the autocomplete list will show Smith and other surnames beginning with 'smi', however, if they do not select an option from the list, I just want to set the value of my observable field to be 'smi'.  At present, the only way the observable propety is set is when the user selects an item from the list of suggestions.</p>
  
  <p>I have the following code (HTML):</p>
  
  <pre><code>&lt;input type="text" data-bind="
                              value: surname,
                              jqAuto: { autoFocus: true },
                              jqAutoSource: surnames,
                              jqAutoQuery: surnameAutocomplete,
                              jqAutoValue: surname" 
              /&gt;
  </code></pre>
  
  <p>JavaScript view model (simplified):</p>
  
  <pre><code>var vm = {
      surnames: ko.observableArray(),
       surname: ko.observable(),
       surnameAutocomplete: function (searchTerm, result) {
          repository.surnameAutocomplete(searchTerm, result);
      };
  </code></pre>
  
  <p>Solution:</p>
  
  <p>I amended the custom binding handler in two places:</p>
  
  <ul>
  <li><p>init: function - added the following</p>
  
  <pre><code>    // New setting to allow / disallow a user to enter values that are in the autocomplete list.
      forceSelection = allBindings.jqAutoForceSelection;
  </code></pre></li>
  <li><p>options change function - amended to the following</p>
  
  <pre><code>//on a change, make sure that it is a valid value or clear out the model value
  options.change = function (event, ui) {
      var currentValue = $(element).val();
  
      // Start: new code, check new setting on whether to force user to make a selection
      if (!forceSelection) {
          writeValueToModel(currentValue);
          return;
      }
      // End: new code
  
      var matchingItem = ko.utils.arrayFirst(unwrap(source), function (item) {
          return unwrap(inputValueProp ? item[inputValueProp] : item) === currentValue;
      });
  
      if (!matchingItem) {                
          writeValueToModel(null);
      }
  }
  </code></pre></li>
  </ul>
  
  <p>I also found that the first item in the autocomplete list was being automatically selected, but then noticed by setting autofocus: false solved my issue, e.g.,</p>
  
  <pre><code>&lt;input type="text" data-bind="                                
                              jqAuto: { autoFocus: false }, /* This fixes the auto select issue */
                              jqAutoSource: surnames,
                              jqAutoQuery: surnameAutocomplete,
                              jqAutoValue: surname,
                              jqAutoForceSelection: false"
              /&gt;
  </code></pre>