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

180
Views
Parece que no puedo definir variables en mi código, porque document.getElementById() no insertará el valor en él

Estoy probando JavaScript y HTML con un poco de CSS (que no es necesario) y parece que no puedo definir esta variable.

Tengo la identificación del elemento con este código HTML:

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

y mételo en una variable con js y este código:

 var made = document.getElementById("iGotThis").value; var total = document.getElementById("outOfThis").value; var perMade = made / total;

y estoy tratando de alertar el resultado con una función de alerta:

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

Aquí está el código completo:

 <!DOCTYPE html> <html> <head> <style> .margin { margin-left: 80px; } </style> </head> <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()"> <script> var iGotThis = document.getElementById("iGotThis").value; let outOfThis = document.getElementById("outOfThis").value; var perMade = iGotThis / outOfThis; // This entire portion has no use for the variables var input = document.getElementById("outOfThis"); input.addEventListener("keyup", function(event) { if (event.keyCode === 13) { event.preventDefault(); document.getElementById("submit").click(); } }); // This is where the useless section ends function perFunction() { alert(iGotThis.value); }; </script> </body> </html>

Intenté simplemente alertar a la variable made o al total , pero solo aparece en blanco. No puedo pensar en una solución, así que espero que alguien pueda ayudar. Todavía soy bastante nuevo en estas cosas y no puedo así todo.

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

0

no hay ID como "total" en el código HTML. por favor revisa eso

about 4 years ago · Juan Pablo Isaza Report

0

El tipo de entrada = cuadro de texto es incorrecto. Puedes usar tipo=texto. Tal vez el navegador no comprenda el valor del cuadro de texto, porque el cuadro de texto no es un valor de tipo HTML oficial. Ve un cuadro de texto, porque el navegador no sabe qué debe usar TI.

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!