How to get the item searched with autocomplete?

0

I have an autocomplete that works perfectly. How do I get the item fetched and put inside a div? Type x-salad research and when you click it put x-salad inside the div. The AutoCompleteSelectHandler (event, ui) function returns nothing.

// BUSCA COM AUTOCOMPLETE
var resultadoLanche = [];
// Captura o retorno do retornaCliente.php
$.ajax({
  url: urlBase + "produtos?filter[categoria_id]=" + idcategoria,
  method: 'GET',
  success: function(retorno)
  {

    retorno.data.forEach(function(item)
    {
      resultadoLanche.push(item.nome);

    });
  },
  error: function(XMLHttpRequest, textStatus, errorThrown)
  {
    alert("Status do Servidor: " + textStatus);
    alert("Erro do Servidor: " + errorThrown);
  }
});

// ativar o autocomplete
$('#buscarProduto').autocomplete(
  {
    source: resultadoLanche,
    select: function(event, ui)
    {
      $('#buscarProduto').val(ui.item.value);
    },
    minLength: 3
  }
);

function AutoCompleteSelectHandler(event, ui)
{
  alert('teste');
var teste = $('#buscarProduto').val(ui.item.value);

}
    
asked by anonymous 07.08.2017 / 20:24

2 answers

0

You do not need to use the AutoCompleteSelectHandler function in this case. Just here in your select

select: function(event, ui)
{
  $('#buscarProduto').val(ui.item.value);
},

You replace the code $('#buscarProduto').val(ui.item.value); with $('#buscarProduto').html(ui.item.value); . If value exists, it will be inserted into the div. Usually val () inserts and captures information from inputs . Take this test and see if you can solve it!

    
08.08.2017 / 23:38
0

Try to replace:

$('#buscarProduto').val(ui.item.value);

By the ID of the respective div and the .text () function

$('#divParaExibir').text(ui.item.value);
    
07.08.2017 / 22:21