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

217
Visualizações
My variables are not being stored, is it possible to fix?

The individual variables are being defined and I can alert them, it's when I want to divide one by the other that, not only it doesn't give me an answer, it doesn't even alert a box!

I have the element id with this HTML code:

<p class="margin"><b>Got </b>
</p><input type="number" class="margin" id="iGotThis">
<p class="margin"><b> out of </b></p>
<input type="number" class="margin" id="outOfThis">

and get it into a variable with js and this code:

   let iGotThis = document.getElementById("iGotThis").value;
   let outOfThis = document.getElementById("outOfThis").value;
   let perMade = iGotThis / outOfThis;

and I am trying to alert the result with an alert function:

    document.getElementById("submit").click();
  }
});
   function perFunction() {
    alert(perMade);
   };

// This entire portion has no use for the variables
var input = document.getElementById("outOfThis");
var perMade = 0;

input.addEventListener("keyup", function(event) {

  let iGotThis = document.getElementById("iGotThis").value;
  let outOfThis = document.getElementById("outOfThis").value;
  let perMade = iGotThis / outOfThis;
  if (event.keyCode === 13) {
    event.preventDefault();
    document.getElementById("submit").click();
  }
});
// This is where the useless section ends


function perFunction() {
  alert(perMade);
};
.margin {
  margin-left: 80px;
}
<!DOCTYPE html>
<html>

<body>
  <br>
  <br>
  <p class="margin"><b>Got </b></p><input type="number" class="margin" id="iGotThis">
  <p class="margin"><b> out of </b></p><input type="number" class="margin" id="outOfThis">
  <br>
  <br>
  <input type="submit" id="submit" class="margin" onclick="perFunction()">
</body>

</html>

Again, the iGotThis and outOfThis variables work, but it is when I try to recall perMade that it just completely breaks.

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

0

The problem is that the variable you are working with is not in scope. As it is being declared from within another function, that is the scope it stays in. To resolve this, you can elevate the scope by declaring the perMade variable at the start of your code.

// This entire portion has no use for the variables
var input = document.getElementById("outOfThis");
var perMade = 0;


input.addEventListener("keyup", function(event) {

  let iGotThis = document.getElementById("iGotThis").value;
  let outOfThis = document.getElementById("outOfThis").value;
  perMade = iGotThis / outOfThis;
  if (event.keyCode === 13) {
    event.preventDefault();
    document.getElementById("submit").click();
  }
});
// This is where the useless section ends


function perFunction() {
  alert(perMade);
};
.margin {
  margin-left: 80px;
}
<!DOCTYPE html>
<html>

<body>
  <br>
  <br>
  <p class="margin"><b>Got </b></p><input type="number" class="margin" id="iGotThis">
  <p class="margin"><b> out of </b></p><input type="number" class="margin" id="outOfThis">
  <br>
  <br>
  <input type="submit" id="submit" class="margin" onclick="perFunction()">
</body>

</html>

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