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

129
Views
Validator says it has no errors and code wont run

Why this code won't run, what is wrong with it? When I try to run it, it just gives me a black page, I've ran it through a HTML validator and it says it's all good. If someone can help me I'd be very grateful.

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title> Area of circle </title>
   </head>
   <body>
      <script type="text/javascript">
         function CalculateArea(){
         
            var r = document.getElementById('form1').value;
            let p = document.getElementById('area')
            var area = (r * r * Math.PI);
            if (r%1 !=0 || r < 1) p.innerHTML = 'Please enter a whole number greater than 0';
            else p.innerHTML = area;
         }  
         <form id='form1'>
            Type radient of circle:
            <input type="text" name="txtr" size=10>
            <br>
            <input type="button" value="Calculate" onClick='CalculateArea()'>
            <p id='area'></p>
         </form>
      </script>
   </body>
</html>

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

The element belongs outside the script element, in the document .

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title> Area of circle </title>
   </head>
   <body>
<form id='form1'>
            Type radient of circle:
            <input type="text" name="txtr" size=10>
            <br>
            <input type="button" value="Calculate" onClick='CalculateArea()'>
            <p id='area'></p>
         </form>
   </body>
   <script type="text/javascript">
       function CalculateArea() {
          var r = document.getElementById('form1').value;
          let p = document.getElementById('area')
          var area = (r * r * Math.PI);
          if (r%1 !=0 || r < 1) p.innerHTML = 'Please enter a whole number greater than 0';
          else p.innerHTML = area;
       }  
   </script>
</html>

about 4 years ago · Santiago Trujillo Report

0

new answer

so in the r variables is not more selecting the whole form

but the only input you need (in the html I assigned a new id for the input)

infact in js now is selecting the input, and getting the .value directly from there :)

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title> Area of circle </title>
</head>

<body>

    <form id="form1">
        Type radient of circle:
        <input type="text" name="txtr" id="r-input" value="10">
        <br>
        <input type="button" value="Calculate" onclick="CalculateArea()">
        <p id="area">
        </p>
    </form>

    <script type="text/javascript ">
        function CalculateArea() {
            var r = document.getElementById('r-input').value;
            let p = document.getElementById('area');
            var area = (r * r * Math.PI);
            if (r % 1 != 0 || r < 1) {
                p.innerHTML = 'Please enter a whole number greater than 0';
                console.log("r " + r);
                console.log("area " + area);
            } else {
                p.innerHTML = area;
            }
        }
    </script>
</body>

</html>

previous answer

sometimes the ide beautify the code wrong,

not because you write wrong,

but because you insert html code in javascript... so technically the ide think that you writing js... (that the result)

no problem, here the solution

  1. copy the <form> code
<form>

    ...

</form>
  1. CRTL X for copy it

  2. put in the start with CTRL C ( outside of <script> tag)

  3. try to delete the spaces between the name of the tag and the < or >

here the code

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title> Area of circle </title>
</head>

<body>

    <form id='form1'>
        Type radient of circle:
        <input type="text" name="txtr" size=1 0>
        <br>
        <input type="button" value="Calculate" onClick='CalculateArea()'>
        <p id='area'>
        </p>
    </form>

    <script type="text/javascript">
        function CalculateArea() {

            var r = document.getElementById('form1').value;
            let p = document.getElementById('area')
            var area = (r * r * Math.PI);

            if (r % 1 != 0 || r < 1) {
                p.innerHTML = 'Please enter a whole number greater than 0';
            } else {
                p.innerHTML = area;
            }
        }
    </script>
</body>

</html>

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