0

I have the following lines in my .js file canvas.js in my RoR /assets/javascripts/ directory.

var b_canvas = document.getElementById("prof_canvas");
console.log(b_canvas);

Which when executed in my browser's console returns.

null

But when I write the following in the Console:

$("#prof_canvas")

I get back the <canvas> object.

Here is the HTML upon which the JS operates:

 <canvas id='prof_canvas' style='width:400px;height:300px '></canvas>
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
Thalatta
  • 4,510
  • 10
  • 48
  • 79

1 Answers1

2

jQuery:

$("#prof_canvas")

Is actually mirror of querySelector:

var element = document.querySelector("#prof_canvas");

And better is to add an script like this at the end of your HTML or code the event when DOM is ready:

<script>
  document.addEventListener("DOMContentLoaded", function(event) {
    console.log("DOM fully loaded and parsed");
  });
</script>

https://developer.mozilla.org/en-US/docs/Web/Events/DOMContentLoaded

Dalorzo
  • 19,834
  • 7
  • 55
  • 102