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

100
Visualizações
Clearing drop-down select value on page exit so back button return may reselect

I am, basically, answering the question here, but perhaps someone has another idea. You can use an onpopstate function, but I find this much simpler to deal with and cross-browser compatible, even with old browsers.

I have a drop-down select box that is populated by a database query. In this case, it has only found one relevant city. Selecting that city, which takes the user to another page, then pressing the Back button, returns the user to the page with the selection still in the selected state, which prevents it from being selected again, unless another selection is made first. So I added an onClick event to change the Selected default before leaving the page.

HTML

<div>
    <select class="DropList"  name="CitySelect" id="CitySelect"
        onchange="if(options[selectedIndex].value){location = options[selectedIndex].value}"
        onclick="GetSelect()">
        <option value="" selected>Select City</option>
        <option value="city.php?s=Montana&cn=Yellowstone&CalNav=0&c=Billings">Billings</option>
    </select>
</div>

JavaScript

<script type="text/javascript">
    function GetSelect()
    {
        document.getElementById('CitySelect').value = "";
    }
</script>
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Listening for the click event is not ideal for a couple of reasons:

  • It's triggered when you click the select to open it, not when you actually select an option. So your code clears the value when you open the select which results in a bit of jerkiness.
  • It's not triggered if you use the keyboard to select a value.

So I'd instead suggest using the blur event:

$('#CitySelect').on('blur', (e) => (e.target.value = ''));

Immediately after selecting the value, the select field will still have the new value but as soon as you leave the field ("blur" it), the value will reset.

Check out this fiddle

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