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

177
Visualizações
Get selected text in searchable dropdown using js

I want to get the selected text in searchable dropdown using javascript function. My expected output is my selected text. But in below code, always return the value as undefined. There's an another problem with this dropdown. When I click one time on the dropdown, it doesn't show the dropdown items. Always, I have to double click to see the dropdown items. My main issue is how to get the seleted text using js function. Hope your help.

document.querySelector('#get-value').addEventListener('click', ()=> {
  var elmValue = document.querySelector('#suggestions').value;
  document.querySelector('#result').innerHTML = elmValue;
})
    <datalist id="suggestions">
         <option>First Option</option>
         <option>Second Option</option>
    </datalist>
    <input class="form-control" autoComplete="on" list="suggestions" id="agentName"/>

<button id="get-value">
Get Value
</button>
<p>
Selected Value: <span id="result"></span>
</p>

Thank you.

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

0

I found a solution for this issue. I included name attribute to the input field.

 <input class="form-control" autoComplete="on" list="suggestions" id="agentName" name="agentName"/>

Then, using the line of code below, I was able to get the selected text.

var selectedValue =  $('[name="agentName"]').val();
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