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

101
Vistas
Outdated value issue in JavaScript form

Here's the HTML:

<input type="text" placeholder="Answer..." id="ansbox1">
<input type="submit" onclick="submit1();" id="sp1">

Here's my JS:

var txt1 = document.getElementById("ansbox1").value;
    
function submit1() {
  if (txt1.split(" ").join("") === atob("[base64string]").split(" ").join("")) {
    alert("Yaay!");
  }
  else { 
    alert("Nope, its wrong!");
  }
}

When I put (even the decoded matching string) in the input box, it still always shows "Nope, its wrong!".

Any error?

I referred to this, yet I still get the same issue... JS - Check if the input field equals the solution

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Since txt1 is outside the function call, it has stale data. Put it inside the function call, so it can be updated everytime.

function submit1() {
  var txt1 = document.getElementById("ansbox1").value;
  if (txt1.split(" ").join("") === atob("[base64string]").split(" ").join("")) {
    alert("Yaay!");
  }
  else {alert("Nope, its wrong!");}
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your txt1 variable is outside the function, which could have a stale/previous value. This variable should be put inside the function so that it's always updated:

function submit1() {
  var txt1 = document.getElementById("ansbox1").value;

  if (txt1.split(" ").join("") === atob("[base64string]").split(" ").join("")) {
    alert("Yaay!");
  }
  else {
    alert("Nope, its wrong!");
  }
}
about 4 years ago · Juan Pablo Isaza Denunciar
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