How to do auto-complete / incremental script

2

There was a need to create a text field that might be given city names, however, if you completed the word in the field when you started the first digits.

I need some idea, function with Arrays literals, something like this:

<script language="javascript">

function autocompletar()
{
cit = ["uberaba","araxa","colombia"];
str = document.all.txt.value;
for (var i in cit)
{
if (document.all.txt.value.length > 1)
{
str.match(document.all.txt.value=cit[i])
               {
            }
        }
    }
}

</script>
  

Dispensing Libraries Nothing personal, just not interested.

    
asked by anonymous 17.04.2016 / 00:01

1 answer

0

Good people, I did ...

  

First, create an array, which will have its compact library where we put the data that will be used sequentially.

     

Soon after, in the function autocompletar() , I defined that the city we want will be one of the three that are in the array, and the one in turn will be represented by the text box. In the body , just create the text field , giving event onKeyup to the function autocompletar() .

//criando objetos array 
sigla = new Object();
valor = new Object();

//declarando array com valores iniciais
  sigla[0] = 1000

//acessando um valor do array
//expecificado diretamente com o operador colchete
sigla[1] = "gua"; valor[1] = "guaíra"

sigla[2] = "bar"; valor[2] = "barretos"

sigla[3] = "mig"; valor[3] = "miguelópolis"
 
function autocompletar()
{
str = document.getElementById('txt').value;

//Iterando sobre aspas duplas de um objeto usando o for..in:
for (var i in sigla)
{

//Acessando o valor de um array através da aspas duplas
if(str.match(sigla[i]))
{

//recuperando os valores de um array entre as aspas duplas
str = document.getElementById('txt').value=valor[i];
           }
      }
}
<input type="text" id="txt" onkeyup="autocompletar()"/>
  

Another alternative in this link , where I did something like this code above, just adapt and ready!

    
18.04.2016 / 08:52