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

280
Visualizações
how to open datalist associated to an input on button click

I have an input type="text" linked to a datalist and a button next to it.
I'm trying to open the datalist when the button is clicked (as if the input box was clicked).
how can I acheive that? it seems like there's no simple solution for this, should I use something other than datalist to make it easier and more supported by browsers?

input::-webkit-calendar-picker-indicator {
    display: none;
}
<input type="text" list="lst" name="input1" />
<button>open list</button><br>
<input type="text" list="lst" name="input2" />
<button>open list</button>
<datalist id="lst">
    <option value="a"></option>
    <option value="b"></option>
    <option value="c"></option>
</datalist>

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

0

   let BTN = document.querySelector('button'),
    List = document.querySelector('datalist'),
    selectBox = document.querySelector('select'),
    input = document.querySelector('input'),
    options = selectBox.options;

   BTN.onclick = ()=>{
        if (List.style.display === '') {
            List.style.display = 'block';
            BTN.textContent = "close list";
            let val = input.value;
            for (let i = 0; i < options.length; i++) {
                if (options[i].text === val) {
                    selectBox.selectedIndex = i;
                    break;
                }
            }
        } else HideSelectBox();
    }

   selectBox.addEventListener('change', ()=>{
       input.value = options[selectBox.selectedIndex].value;
       HideSelectBox();
   });


    input.addEventListener('focus', HideSelectBox);

    function HideSelectBox () {
        List.style.display = '';
        BTN.textContent = "open list";
    }
        select {
            width: 185px;
        }
        datalist {
            display: none;}

        option {
            padding: 3px;
        }
        option:hover {
            background-color: #cccc;
        }
        input::-webkit-calendar-picker-indicator {
            display: none;
        }
<label>
    <input list='lst'>
</label>
<button>open list</button>

<datalist id="lst">
    <label>
        <select multiple size=3>
            <option value="a">a
            <option value="b">b
            <option value="c">c
        </select>
    </label>
</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