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

187
Views
Javascript: use el número de retorno de una función para calcular el total

He separado mi formulario en diferentes funciones y aunque cada una funciona, no encuentro la forma de usar el valor devuelto en la función total.

 function adult() { let a = document.getElementById("adulte").value; let t = a * 100 document.getElementById("asub").innerHTML = t //document.getElementById("sub-a").value = t; console.log(t); document.getElementById("tprice").innerHTML = a; return t; } function total() { let a = adult(); document.getElementById("tprice").innerHTML = a; console.log(a); }
 <input id="adulte" name="adulte" style="width: 40px" onchange="adult();" /> <span id="aprice"> 100</span>$/nuit <p> Hébergements pour les adultes = <span id="asub">100</span>$</p> <button id="total" name="total" onclick="total();">calculate</button> <span id="tprice"> 50</span>$

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Creo que el siguiente código debería funcionar, pero recomiendo usar la etiqueta de form .

No desea activar adult() cada vez que cambie la entrada, lo que creo que es innecesario y redundante. todavía hay mucho espacio para la optimización del código, pero no quiero estropear su método de hacer las cosas.

También en html5, el evento onchange se activa en dos condiciones:

1. Al pulsar la tecla Intro
2. al perder el foco (en el desenfoque)

 <html> <head> <script> function adult(input) { let a = input; document.getElementById('asub').innerHTML = a * 100; document.getElementById('tprice').innerHTML = a; return t; } function total(event) { let a = adult(event.target.value); document.getElementById('tprice').innerHTML = a; } </script> </head> <body> <input type="number" id="adulte" name="adulte" style="width: 40px" onchange="total(event)" min=0 /> <span id="aprice"> 100</span>$/unit <p>Hébergements pour les adultes = <span id="asub">100</span>$</p> <button id="total" name="total" onclick="total();">calculate</button> <span id="tprice"></span> </body> </html>

Código Stackblitz agregado: haga clic aquí

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!