Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda