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

203
Visualizações
How do I return a value from a function into a variable in javascript then display it using HTML?

I am very new to JS and I have been struggling with this for a few hours now.

<script>
document.getElementById("myButton").onclick = function myFunction()
{
  var Ra = document.getElementById("input1").value;
  var Rb = document.getElementById("input2").value;
  var Ea = (1) / ( (1) + (Math.pow((10), ((Rb - Ra) / (400)))) );
  var Eap = Ea * 100;
  return Eap;
}
var result = myFunction();
</script>

<div class="result"> 
Your probability of winning is: <script> document.write(result); </script>
</div>

Basically, I have taken two values from the user, sent it to the function for some calculations. I want to use the value of the variable 'Eap' to display the result of the calculations one way or another. I cannot use the variable directly for I assume its scope is within the function only? And I cannot store the return value of the whole function. enter image description here

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

0

Just use .textContent.

As it's 2022 I changed .getElementById to .querySelector and var to const.

document.querySelector('#button').addEventListener('click', () => {
  const Ra = document.querySelector('#input1').value;
  const Rb = document.querySelector('#input2').value;
  const Ea = (1) / ((1) + (Math.pow((10), ((Rb - Ra) / (400)))));
  const Eap = Ea * 100;
  document.querySelector('#result').textContent = Eap;
})
<input id="input1">
<input id="input2">

<button id="button">update</button>

<p>result: <span id="result"></span></p>

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