Clear Autocomplete data when new item is searched

Clear Autocomplete data when new item is searched

Hi,

I am trying to use Autocomplete in SharePoint custom list. I have used the below code for the autocomplete functionality

  1. $(document).ready(function() {
  2. var titles = [];
  3. var autocomplete = $("input[title='Skill Required Field']").autocomplete({
  4.   minLength: 3,
  5.   source: function(request, response) {
  6.     $.ajax({                            
  7.       url: siteURL + "/_api/web/lists/getbytitle(\'MySkills\')/items?$select=Title&$filter=startswith(Title, '" + request.term + "')",
  8.       cache: false,
  9.       method: "GET",
  10.       headers: { 
  11.         "Accept": "application/json; odata=verbose" 
  12.       },
  13.       success: function(data) {
  14.         $.each(data.d.results, function(i, result) {
  15.           if (result.Title) {
  16.             titles.push(result.Title)
  17.           }                             
  18.         });
  19.         return response(titles);
  20.       },
  21.       error: function( data ) {
  22.         alert('search error');
  23.       }
  24.     });
  25.   },
  26.   focus: function(event, ui) {          
  27.     return false;
  28.   }
  29. });
  30. });


The above code works fine, but the issue is - If i search for a term and select it and if I again search for another term, then in the search results collection, the first searched result is also showing up. How to clear the search result after each record selection.
I am using jquery-1.11.3.min , jquery-ui-1.12.1