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

164
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report

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 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!