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

130
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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