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

249
Views
Cómo manejar la entrada de un cuadro de texto para no imprimir un NaN

Estoy tratando de obtener la entrada del usuario del cuadro de texto y luego hacer clic en el botón al lado para calcular el área de un círculo. El problema es que la alerta siempre imprime NaN.

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML 5 Boilerplate</title> <link rel="stylesheet" href="./css/style.css"> <script> const PI = 3.14159; function getAreaOfCircle(rad) { let area = PI * Math.pow(rad, 2); alert(area); } </script> </head> <body> <input type="text" placeholder="Type something..." id="myInput"> <input type="button" onclick="getAreaOfCircle();" value="Calculate Area of a Circle"> </body> </html>

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Tienes que pasar el valor de entrada en tu función. Como su código para pasarlo a su función como argumento/parámetro. Entonces puedes codificarlo así:

 <input type="button" onclick="getAreaOfCircle(document.getElementById('myInput').value);" value="Calculate Area of a Circle">
about 4 years ago · Santiago Gelvez Report

0

Debe obtener el texto de entrada real y convertir su valor en un número flotante:

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML 5 Boilerplate</title> <link rel="stylesheet" href="./css/style.css"> <script> const PI = 3.14159; function getAreaOfCircle() { //Get the radius from the input field let rad = document.getElementById("myInput").value; //Check if the input is not empty, if it is set rad to 0 rad = rad !== "" ? parseFloat(rad) : 0; let area = PI * Math.pow(rad, 2); alert(area.toFixed(2)); } </script> </head> <body> <input type="text" placeholder="Enter the circle radius" id="myInput"> <input type="button" onclick="getAreaOfCircle();" value="Calculate Area of a Circle"> </body> </html>
about 4 years ago · Santiago Gelvez Report

0

No está pasando rad en la función getAreaOfCircle

Entonces puede obtener valor dentro de la función getAreaOfCircle como:

 const rad = document.querySelector("#myInput").value; 

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>HTML 5 Boilerplate</title> <link rel="stylesheet" href="./css/style.css"> <script> const PI = 3.14159; function getAreaOfCircle() { const rad = document.querySelector("#myInput").value; let area = PI * Math.pow(rad, 2); alert(area); } </script> </head> <body> <input type="text" placeholder="Type something..." id="myInput"> <input type="button" onclick="getAreaOfCircle();" value="Calculate Area of a Circle"> </body> </html>

about 4 years ago · Santiago Gelvez 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!