Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

165
Views
Cómo pasar valor de javascript a html

Estoy tratando de pasar el valor de javascript a html pero no puedo hacerlo, usé return en la función js pero probablemente no correctamente

 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 answers
Answer question

0

Su llamada de función está sucediendo mucho antes del evento de clic (en la carga de la página), en lugar de devolver el valor de la función, puede actualizar el contenido del elemento dentro de la función misma.

Además, le sugiero que use innerText o textContent para configurar el contenido si es una cadena simple (no htmlString ).

Puedes probar de la siguiente manera:

 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 Report

0

Mientras llama a add() desde el botón html onClick, puede necesitar actualizar DOM desde la función de agregar con 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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!