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

102
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 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