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

166
Views
JavaScript básico cómo llamar a una función desde la página HTML

Acabo de comenzar con los conceptos básicos de JavaScript y estoy luchando con esto: la página HTML tiene lo siguiente:

 <body> <p id="number">5</p> <button id="buttonX" onclick="calcSquare()">Click here!</button> </body>

Esta página debe llamar a la función calcSquare() , que obtiene el valor del elemento, calcula su cuadrado e imprime en la consola: The square of 5 is 25 . La página HTML carga el código, por lo que puedo referirme a la página con la palabra clave del documento. Mi código js es el siguiente:

 function calcSquare() { var number = document.getElementById("number").value; console.log("The square of" + number + "is" number*number); }

¿Alguien puede decirme qué tiene de malo? ¡Muchas gracias! Luchas de principiantes...

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

0

El problema es con su propia función.

Tenga en cuenta que .value se usa con un elemento de entrada. Entonces puede usar .innerHTML o .textContent .

También olvidaste el signo + después de "es".

Mira este fragmento de código.

 function calcSquare() { var numbElementcontent = document.querySelector('#number').innerHTML; numbElementcontent = parseInt(numbElementcontent); console.log("The square of " + numbElementcontent + " is " + numbElementcontent * numbElementcontent); }
 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p id="number">5</p> <button id="buttonX" onclick="calcSquare()">Click here!</button> <script> function calcSquare() { var numbElementcontent = document.querySelector('#number').innerHTML; numbElementcontent = parseInt(numbElementcontent); console.log("The square of " + numbElementcontent + " is " + numbElementcontent * numbElementcontent); } </script> </body> </html>

about 4 years ago · Juan Pablo Isaza Report

0

En lugar de .value use .innerHTML , y le falta el signo + después de "is"

about 4 years ago · Juan Pablo Isaza Report

0

Necesitas obtener textContent y hacer algunos cambios en tu función calcSquare()

 function calcSquare() { var number = document.getElementById("number"); var number = number.textContent; console.log("The square of" , number , "is" , number*number); }
 <body> <p id="number">5</p> <button id="buttonX" onclick="calcSquare()">Click here!</button> </body>

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!