Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
¿Cómo puedo seleccionar el elemento del menú desplegable de búsqueda y hacer que rellene el campo de entrada de búsqueda?

Si escribe una palabra en el campo de entrada de búsqueda, aparece una lista de sugerencias a continuación.

ingrese la descripción de la imagen aquí

Me gustaría poder seleccionar la palabra de la lista, por ejemplo, Maine (ME) y luego completar el campo de entrada de búsqueda. ¿Cómo manipularía mi código para poder hacer esto, por favor?

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 answers
Answer question

0

Encontré que necesitaba poner value="" en el archivo html

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

y para actualizar la siguiente función en el archivo javascript.

 function myFunction() { let texts = document.querySelector("h6").innerText; document.getElementById("search").value = texts; matchList.innerHTML = ''; }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!