Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

157
Visualizações
jQuery autocomplete + ajax API call only triggers list on spacebar/down arrow?

I am making an Ajax request to a music search API, using the jQuery autocomplete feature. The call works great, but I believe I have to do some Ajax adjustments/enhancements. The autocomplete feature seems to work great every time the space bar is clicked, which is quite odd?

If I type in the letters "lose", I get the full song list output to my console, but the dropdown suggestion box does not show, unless I hit the space bar, or if I hit the down arrow on my keyboard?

function songSelection() {


    $(".song-input").each(function () {
        let input = $(this);
        if (input.next() == 'SongSelector') {
    
            let timer;

            input.on('keydown', function () {
                timer = setTimeout(doneTyping, 300);
            });

            function doneTyping() {
                let songSearch = input.val()
                $.ajax({
                    url: `https://deezerdevs-deezer.p.rapidapi.com/searchq=${songSearch}`,
                    method: "GET",
                    headers: {
                        "x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
                        "x-rapidapi-key": "APIkeyhere"
                    },
                    success: function (data) {
                        let songList = []
                        for (let item in data.data) {
                                songList.push(data.data[item].song)
                        }
                        $('.song-input').autocomplete({
                            source: songList,
                        })

                    },
                })
            }
        }
    });
}

Am I missing something in the Ajax request? Just seems like pretty odd functionality to me that the dropdown autocomplete suggestions seem to really only want to work when the spacebar or down arrow key is hit after typing?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Consider the following.

$(function() {
  $(".song-input").autocomplete({
    delay: 300,
    source: function(request, response) {
      var songSearch = request.term;
      var songList = [];
      $.ajax({
        url: "https://deezerdevs-deezer.p.rapidapi.com/",
        data: {
          searchq: songSearch
        },
        method: "GET",
        headers: {
          "x-rapidapi-host": "deezerdevs-deezer.p.rapidapi.com",
          "x-rapidapi-key": "APIkeyhere"
        },
        success: function(data) {
          $.each(data.data, function(key, value) {
            songList.push(value.song);
          });
          response(songList);
        },
      })
    }
  });
});
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<div>
  Search: <input type="text" class="song-input">
</div>

This will assign Autocomplete to all Elements with class song-input. When the User enters a search term, request.term, it will be passed to the API and the results will be formatted into an Array. That array will be passed to response().

See More: https://api.jqueryui.com/autocomplete/#option-source

about 4 years ago · Juan Pablo Isaza Relatório

0

You're repeatedly setting up timers that don't actually debounce the typing. So the behavior of your done typing function isn't to actually detect when someone is done typing.

See this answer for more information: https://stackoverflow.com/a/57763036/637283

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda