Input autocomplete

Viewed 84

I tried to do autocomplete for search input. I wrote code below.

 <script>
 var availableTags = [];
  $( function() {
    $( "#search" ).autocomplete({
      source: availableTags
    });
  } );

    $(document).ready(function(){
        $("#search").on("input",function(e){
            console.log($("#search").val())
            $.ajax({
                method:"post",
                url:"/livesearch",
                data:{search:$("#search").val()},
                success:function(res){
                    availableTags = res['res'];
                    console.log(availableTags);
                }
            });
        });
    });
  </script>

res['res'] is array. I tried to update availableTags after i get some values from database, but it doesn't work. When i type some text which contains in my tags, i don't see anything. How to fix it?

Second part of my question How to customize autocomplete. For example, add to it photos(example: tommy.com, if you click on search icon and type something, you will see custom autocomplete)

UPD: I resolved problem with ajax and tags. But second question is still available. How to make custom suggestions for input's autocomplete?

0 Answers
Related