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

83
Visualizações
Hide value from datalist selection

I am using datalist to display a selection of choices. When showing these choices both tha value and text are displaying, like datalist is supposed to work. However, I need to hide the value from the list, onsly showing text.

After a lot of searching I have found out that using data-value is supposed to work, instead of just value. The problem is that I dan't get it to work. The reason I am using datalist is that I need to have it in order to get a functional searchable dropdown. If it wasn't for that I would have dropped it.

My code is as follows:

var myinput = document.getElementById('example');
var divs = document.querySelectorAll('.hidden');
myinput.addEventListener('change', function() {
    var mydiv = document.getElementById(this.value);
    divs.forEach(div => {
      div.style.display = div.id === this.value ? 'block' : 'none';
    });
});
.hidden {
  display: none;
}
<fieldset>
    <legend>This is a datalist</legend>
    <input type="text" id="example" list="brow" />
    <datalist id="brow">
        <option value="div1">Choice 1</option>
        <option value="div2">Choice 2</option>
    </datalist>
</fieldset>

<div class="hidden" id="div1">This is div1</div>
<div class="hidden" id="div2">This is div2</div>

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

0

It gives priority to your value in dataList, please make sure your option value and option text remain same so that duplication can be avoided

<fieldset>
<legend>This is a datalist</legend>
<input type="text" id="example" list="brow" />
<datalist id="brow">
    <option value="Choice 1">Choice 1</option>
    <option value="Choice 2">Choice 2</option>
</datalist>
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