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

198
Visualizações
How can I select the item from the search dropdown and have it populate the search input field

If you type a word into the search input field a suggestion list appears below.

enter image description here

I would like to be able to select the word from the list e.g Maine (ME) and have it then populate the search input field. How would I manipulate my code to be able to do this please?

HTML

<form action="*">
  <div class="form-group">
    <input type="text" class="rounded form-control" id="search" autocomplete="off" 
    placeholder="Current Location">
  </div>
<div id="match-list" onclick="myFunction()"></div>
</form>

javascript

const search = document.getElementById('search');
const matchList = document.getElementById('match-list');

// Search states.json and filter it
const searchPlaces = async searchText => {
    const res = await fetch('../data/states.json');
    const states = await res.json();

    // Get matches to current text input
    let matches = states.filter(state => {
        const regex = new RegExp(`^${searchText}`, 'gi');
        return state.name.match(regex) || state.abbr.match(regex);
    });

    if (searchText.length === 0) {
        matches = [];
        matchList.innerHTML = '';
    }
    outputHtml(matches);
};

// Show results in HTML
const outputHtml = matches => {
    if (matches.length > 0) {
        const html = matches.map(match => `
        <div class="card card-body mb-1">
        <h6>${match.name} (${match.abbr})</h6>
        </div>
        
        `).join('');
        matchList.innerHTML = html;
    }
};

search.addEventListener('keyup', () => searchPlaces(search.value));

function myFunction() {
    document.getElementById("search").innerHTML = "text";
  }

json

[
    {
        "abbr": "AL",
        "name": "Alabama",
    },
{
        "abbr": "ME",
        "name": "Maine",
    }
]

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

0

Found I needed to put value="" into the html file

 <input type="search" class="rounded form-control" id="search" autocomplete="off" placeholder="Current Location" value="">

and to update the following function in the javascript file.

function myFunction() {
    let texts = document.querySelector("h6").innerText;
    document.getElementById("search").value = texts;
    matchList.innerHTML = '';
  }
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