How to add your js code to html, with files?

I want to add js code to the html.

Hi @amejl172!

Are you referring to adding a script tag to your html page and writing js in there?

1 Like

In Atom for example I have html code and JS code. I want to link the JS to the html, so I wirte the code on the JS file, and only use the html for browser and size.

You can create an external js script and link it with your html page.
You can call it index.js.

Here is an example from one of my projects.

2 Likes

I have <script></script> with my source to file, but, In the js
I have this code var LC = document.getElementById('LineChart');
I donā€™t know if it links, because nothing happens in the browser.

Are you doing something with that variable?

Right now it looks like you are just getting an id but not doing anything with it.

1 Like

I have this code for it
LC = new Chart("LineChart", { type: 'line', data: data, options: options });

Iā€™m trying to make a line chart, so I can see it in the browser but, I donā€™t know how to link it to html.

Well you can open up the console in dev tools and check for error messages.

1 Like

I have this error

ReferenceError: Chart is not defined

It is probably referring to this.

In the error messages, it will tell you which line in the js script the error is.

1 Like

LC = new Chart(ā€œLineChartā€, {
^

they say this, but I have var LC in code already.

Where did you define Chart though?

1 Like

I donā€™t have defined it.

Well thatā€™s what the error message says.

1 Like

If I define it I need to make it to a variable. But I were expecting it to be a command, because Iā€™m new to programming.

The computer is looking for something called Chart and it canā€™t find it so you need to define it.

1 Like

How do you define it?
I know when defining itā€™s var, but this one I donā€™t understand why to make it a variable.

Well you can create a new object called linechart and assign this to it.

But I am not entirely sure why you want to assign this object to this:

document.getElementById('LineChart').

What are you trying to accomplish here?

1 Like

When I watch YouTube and looked at CHART.JS, this is what I thought I had to do.
I donā€™t understand what the code does exactly.

I will read again.

Itā€™s I think to get html code <canvas id = "LineChart", height = 400px, width = 1832px></canvas>

I am not familiar with that library but I would go through documentation.

1 Like