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

137
Visualizações
open a long <select> dropdown to specific option when the dropdown is first clicked

I have a large dropdown list containing possible answer like this:

<select name="mySelect" id="mySelect" required="" >
        <option value="-100"  > -100% </option>
        <option value="-99"  > -99% </option>
        <option value="-98"  > -98% </option>
        <option value="0"  > 0% </option>
        <option value="101" selected = "" hidden = ""> ----- </option>

going from -100 to 100. When the dropdown is first clicked, I would like it to open (and select) at a prespecified value, i.e. at 0.

I have tried this:

$('#mySelect').one('focus', function() {
    $(this).children('option[value=0]').prop('selected',true);
});

and this works well in Chrome, but it doesn't work in Firefox, which is a problem for me. (edit) As mentioned in the comments, in Firefox above script selects the correct option, but dropdown is not scrolled to that value.

Help with a working solution in both Firefox and Chrome would be greatly appreciated.

Here is a fiddle of the problem: https://jsfiddle.net/monni/an4guvse/28/

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

0

I don't want to show any "real" value before the dropdown is clicked

Given this statement, in the comments, what you're attempting to do is not workable. The actions available on an options list within a select are vey limited, and this behaviour is non-standard.

I'd argue it's even counter intuitive and will confuse your users and cause more problems than it solves.

What you're attempting to achieve seems like it could be better solved by using a range input:

$('#foo').on('input', e => $(e.target).next().text(e.target.value + '%'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<input type="range" min="-100" max="100" value="0" id="foo" />
<span></span>

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