Autocomplete not working when the data is dynamically created using on click of the textbox

Autocomplete not working when the data is dynamically created using on click of the textbox

Hi All,
 
I Have a Strange problem going on for weeks and I don't know how to fix it.
 
I have two text boxes City and Area which needs to show autocomplete.
 
While the page loads, Im taking all the city which I need from database and saving it in an arraylist.
 
So when the user types a City, the autocomplete works perfectly.
 
Problem comes after this, based on the City the user has choosen, when he clicks the Area textbox, Im making an ajax call and getting the areas for the city in from the database and saving it in an arraylist.
But when the user types the area, autocomplete is not working even though values are present in the araylist.
I am sure the ajax call works fine as I gave alert and checked the values.
 
I am not sure whether autocomplete works before the ajax call itself or something else is going wrong.
 
Please help me to solve this problem.
 
Thanks in Advance.
 
 
 
  1. <script>

  2. var list;
    var list1;
    var list2;
    var a;
  3. $(document).ready(function(){
  4. $('.area').click(function() {
  5.      var id = $(this).attr('id');
         var city = id.replace('PRODUCER', 'BRANCH');
         var cityvalue = $('#' + city).val();

  6.      if(cityvalue.length!=3){
        
         alert('cityvalue field cannot be left empty\nPlease enter Branch');
          $('#' + city).focus();
          return true;
         }
       
     $.get('<%=request.getContextPath()%>/CsGateway?viewName=view1&&actionName=action1&&dow='+document.getElementById('<%=CiViewBeanKeys.dow%>').value+'&&cityvalue='+cityvalue,function(data,status){
     
  7.  
  8. list =data.split("&&")[0];
     
     
     
    list1= $.trim(list);
  9. a = $.parseJSON(list1);
  10.  });
     
  11.    
       
       
    });

  12.  

  13. });
  14. var city =[
    <% for(int a = 0;a<branch.size();a++){
    cvCity obj = (cvCity)branch.get(a);
  15. %>
  16.  '<%= obj.cityList() %>'
     
     <% if(a<branch.size()-1){ %>
     ,
    <%} %>
  17. <% } %>]
  18. <!-- Autocomplete -->
     
     $(function () {
        $(".city").autocomplete({
       
            source: function (request, response) {
                var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
                response($.grep(city, function (item) {
                    return matcher.test(item);
                }));
            },
            minLength: 1
            });
           
     
  19.  $(".area").autocomplete({
  20.         source: function (request, response) {
                var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
                response($.grep(a, function (item) {
                    return matcher.test(item);
                }));
            },
            minLength: 3   
               
          
        }); 
     
     
         
    });