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

179
Visualizações
JS Onchange display value before processing

I have a form with a select input element. After the user selects an option, I check that option chosen. If it is bigger than I expected, I bring up an alert box asking the user if they're sure they selected the right option. This is just a warning, so I don't reset the value or prevent the user from selecting these options.

Currently, this causes the alert to display first, and then the option on the webpage is changed AFTER they hit OK.

How can I get the value in a select element to display the selection before I process it during the onchange event?

mySelectElement.onchange = function() {
    if(mySelectElement.value >= 1000) {
        alert("Are you sure you can eat that many apples?");
    }
    // else nothing, less than 1000
};
about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

A couple solutions, one don't use an alert. You can use a modal library, or just output to the dom.

If you must use an alert,wrap it in a setTimeout so its delayed.

let select = document.querySelector("select");

select.addEventListener("change",function(){
  if(select.value > 1){
    setTimeout(function(){alert("Alert");},100);
  } 
});
<select ><option value="0">A</option><option value="3">3</option></select>

about 4 years ago · Santiago Trujillo 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