Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

165
Vistas
Basic JavaScript how to call a function from the HTML page

I have just started JavaScript basics and I'm struggling with this: HTML page has the following:

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

This page should call the function calcSquare(), which fetches the value of the element, calculate its square, and print in the console: The square of 5 is 25. The HTML page loads the code, so I can refer to the page with the document keyword. My js code is following:

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

Can someone tell me what is wrong with it? Thank you so much! Beginners struggles...

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

The problem is with your function itself.

Keep in mind that .value is used with an input element. So you can use .innerHTML or .textContent.

You forgot also the sign + after "is".

Look at this code snippet .

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 Denunciar

0

Instead of .value use .innerHTML, and you are missing sign + after "is"

about 4 years ago · Juan Pablo Isaza Denunciar

0

You need to get textContent, and make some changes in your function 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda