Chartjs-Are you required to make an http request when trying to use Chart.js?

0👍

The HTTP call is not part of anything related to Chart.js.

The reason why it’s not working is you’re calling "getElementById" before the DOM element you’re looking for has been loaded.

The reason why it works when you are using the fetch call is that while you are making the HTTP request, you are giving the DOM time to load your canvas.

It’s usually best to wait for your whole DOM to load before running any javascript.

Solution

Move the script tag to the bottom of the page
OR
Add "defer" to your script tag which will tell the browser to start running the javascript after the DOMContent has been parsed.

Leave a comment