Skip to content
Justice Tech School

HTML and JavaScript

JavaScript goes inside a script element, either inline or loaded from a file with src. Put scripts at the end of the body, or use the defer attribute, so the page loads first.

Elements find each other through ids. A button can call a function with onclick, but addEventListener is the cleaner approach.

Example
<p id="msg">Waiting...</p>
<button onclick="document.getElementById('msg').textContent = 'Hello from JavaScript!'">Click me</button>

Exercise

Add the element that holds JavaScript.

<>console.log("hi");</>

Quiz Test

Why put a script at the end of the body?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.