Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

67
Vistas
Alert before executing value on select

I have a Select input type with 3 options and want to prompt an Alert window when any option is selected.

This alert has to put the execution of the selected option on hold and give a warning like: "Unsaved changes will be lost. Are you sure you want to continue?", then I want the option to 'Cancel' the execution or to press 'OK' and execute.

How can I add a Cancel option inside the alert so it doesn't execute the selected option and stays on the previously selected option?

See my code below and in the JSFiddle

HTML:

<p> Choose your formation: </p>
<select id="formation" name="formation" onchange="myFunction(formation)">
  <option value="1"> 1-1-2 </option>
  <option value="2"> 1-2-1 </option>
  <option value="3"> 2-1-1 </option>
</select>

<p>
  You chose the following formation: <spam id="demo"> X</spam>.
</p>

Javascript:

function myFunction(formation) {
    alert("Unsaved changes will be lost. Are you sure you want to continue?")

    if(document.getElementById('formation').value == "1") {
    document.getElementById("demo").innerHTML = "1-1-2";
  }
  if(document.getElementById('formation').value == "2") {
    document.getElementById("demo").innerHTML = "1-2-1";
  }
  if(document.getElementById('formation').value == "3") {
    document.getElementById("demo").innerHTML = "2-1-1";
  }
}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda