0
I’m using the typeahead, which works as follows, every text I type in the field it takes the value of the field, goes to a database, searches and creates a dropdown with the values found.
It works perfectly, but I wanted as soon as the enter button was pressed the field completed with the first value of the dropdown.
I can get the first number with:
var valor = $(".tt-suggestion p").first().text();
But when trying to set it in the field using:
$("#cidade_estado").val(valor);
or
$("#cidade_estado").val(valor);
Nothing happens.
I know he’s getting into the if since he does the:
$("#telefone").focus();
Normally and displays the values in the console.
$(document).ready(function () {
    $('input.cidade_estado').typeahead({
        name: 'municipio',
        remote: 'action/getMunicipios.php?query=%QUERY'
    }).keypress(function (e) {
         var valor = $(".tt-suggestion p").first().text();
         console.log("Aqui: " + valor);
        if (e.which == 13) {
            console.log("13: " + valor);
            $("#cidade_estado").val(valor);
            $('#cidade_estado').attr('value', valor);
            $("#telefone").focus();
            return false;
        }
    });
});
html:
            <form id="formemail" action="action/enviaemail.php" method="post">
                <input name="nome" type="text" placeholder="Nome completo">
                <input name="empresa" type="text" placeholder="Empresa">
                <input id="cidade_estado" name="cidade_estado" class="cidade_estado" type="text" placeholder="Cidade">
                <input name="telefone" class="grupocontato" id="telefone" type="text" placeholder="Telefone">
                <input name="email" class="grupocontato" type="text" placeholder="Email">
                <textarea name="mensagem" placeholder="Mensagem"></textarea>
                <input class="btnContato" type="submit" value="Enviar">
            </form>
Console:

Is the value being printed normally in the console.log? You could also put the HTML snippet?
– Renato Diniz
The valos is shown normally in the console, I also tried to put any string there and gives the same effect, ie nothing.
– Wictor Chaves
Have you tried setting yourself ? $(this). val(value)
– AnthraxisBR
I didn’t, but I tested it here just now and it didn’t work.
– Wictor Chaves
What
console.log("13: " + valor);display?– Guilherme Nascimento
makes the following test:
console.log("13: " + valor, $('#cidade_estado').length);.– Guilherme Nascimento
The result was this: "13: São Francisco do Guaporé, RO 1", that is to say, he is not really doing this.
– Wictor Chaves
I made the following console.log("13: " + value, $(this).text().length); it returned 25, ie it arrow, but somehow the plugins does not show or remove this value.
– Wictor Chaves
Ah, you want to fill in the plugin field ? need to trigger a click on the value in question, see which element identification typeahead renders on the input, and trigere a click on the value you want in view of trying to give a val(value), think so will fill
– AnthraxisBR
@Anthraxisbr if you want to post the reply so I can mark it as answered.
– Wictor Chaves