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

154
Visualizações
How to trigger an event on a <select> element when the same value is selected?

The event type change will be triggered when a different item is selected from the combo box, but won't be triggered when the same item is selected again. What other event type or trick/loophole will get me the desired action?

const options = document.querySelector('#select');
options.addEventListener('change', function() { // won't be triggered when chosen the item chosen is the same
  console.log(this.value);
});
<select id="select">
  <option value="1" selected>1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

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

0

Default and expected behaviour of a select, is the change event triggering when you CHANGE the select. If you just look at the options and choose the same option that was already selected, then no event is expected. So if you need to trigger each time, then you need to use the use click

In any case add a "Please select" to allow the event to trigger on the first actual options

document.querySelector('#select').addEventListener('click', function() { 
  if (this.value) console.log(this.value); 
});
<select id="select">
  <option value="" selected>Please select</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

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