javascript - Predictive text search box -


i looking predictive search box performs example provided codpen below.

how edit code instead of predicting word 'california', have array of strings predicts for?

additionally possible make once word complete, script attempts predict next word being typed?

https://codepen.io/sivarp18/pen/ewbmx

$("#search").keyup(function(){    $("#background-input").val('');    var city = "california";    var searchtext = $("#search").val();    console.log(searchtext.tolowercase());    if(searchtext === "")      $("#background-input").val('');    else if(city.indexof(searchtext.tolowercase())==0)      {        var currentlength = searchtext.length;        $("#background-input").val(searchtext+""+city.substring(currentlength));           }  });
#search-div  {    position: relative;  }  #background-input  {    position: absolute;    color: #999999;    height:25px;    width:250px;  }  #search  {    position: relative;    background: transparent;    height:25px;    width:250px;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <div id="search-div">    <input type="text" id="background-input" />    <input type="text" id="search" placeholder="type california.." />  </div>

you have implement loop in existing code after else/if statement loop through each of cities in array, , execute following code on each of strings in array.

this code works me -

$("#search").keyup(function(){   $("#background-input").val('');   var cities = ["california", "london", "paris"];   var searchtext = $("#search").val();   console.log(searchtext.tolowercase());    if(searchtext === "")     $("#background-input").val('');   else {       for(var = 0; < cities.length; i++){         if(cities[i].indexof(searchtext.tolowercase())==0)             {             var currentlength = searchtext.length;             $("#background-input").val(searchtext+""+cities[i].substring(currentlength));              }     }   } }); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -