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

164
Visualizações
How to pass value from javascript to html

I'm trying to pass value from javascript to html but I can't do it, I used return in js function but probably not correctly

function add() {
  var a3 = document.getElementById('a').value;
  return a3;
}
var a5 = add();
document.getElementById("greeting").innerHTML = a5;
<form>
  a: <input type="number" name="a" id="a"><br>

  <button onclick="add()">Add</button>
  <p id="greeting">Greetings</p>

</form>

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

0

Your function call is happening way before the click event (on page load), instead of returning the value from function you can update the element's content inside the function itself.

Also, I will suggest you use innerText or textContent to set the content if it is simple string (not htmlString).

You can try the following way:

function add() {
  var a3 = document.getElementById('a').value;
  document.getElementById("greeting").textContent += " " + a3;    
}
<form>
  a: <input type="number" name="a" id="a"><br>

  <button type="button" onclick="add()">Add</button>
  <p id="greeting">Greetings</p>
</form>

about 4 years ago · Juan Pablo Isaza Relatório

0

As you are calling add() from html button onClick, you can need to update DOM from add function itself with innerText

function add() {
  var a3 = document.getElementById('a').value;
  document.getElementById("greeting").innerText = a3;
}
a: <input type="number" name="a" id="a"><br>

<button onclick="add()">Add</button>
<p id="greeting">Greetings</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