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

163
Vistas
null error when using document.getElementById()

I'm trying to to draw a circle using the canvas tag using javascript, but I am constantly getting an error stating c is null when using document.getElementById(). I initially assumed this was because of the HTML content not loading fast enough, so I used window.onload but this did not change anything. Any help would be appreciated:

HTML:

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Title</title>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <meta name="description" content="" />
        <link rel="stylesheet" href="css/stylesheet.css" />
        <link rel="icon" href="images/favicon.png" />

        <script src="javascript/script.js"></script>
    </head>
    
    <body>

        <canvas id="myLogo" width="150" height="150"></canvas>
        
    </body>
</html>

Javascript:

window.onload = scripting();

function scripting() {

    logo();

    function logo() {

        console.log('called logo()');
        
        var c = document.getElementById("myLogo");
        var ctx = c.getContext("2d");
        ctx.beginPath();
        ctx.arc(100, 75, 50, 0, 2 * Math.PI);
        ctx.stroke(); 
    }
}

The above code results in the following error, referencing lines 12:13:

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

0

Have you tried moving your <script> block to the bottom of (last item in) <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