HTML gives structure, CSS gives style, and JavaScript gives behaviour — the third and final layer. This lesson is the bridge: just enough to see how HTML connects to JavaScript, so the dedicated JS course feels natural.
The script element
JavaScript is added with the <code><script></code> element. You can write code directly inside it, or link an external <code>.js</code> file with the <code>src</code> attribute — the same idea as linking CSS.
Where to put the script
Placement matters. The common best practice is to put <code><script></code> just before the closing <code></body></code> tag. That way the HTML loads and displays first, and the script runs once the elements it needs already exist on the page.
Connecting JavaScript to HTML with id
JavaScript usually works by grabbing an element and changing it. The most common way is by <code>id</code>: give an element an id in HTML, then find it in JavaScript with <code>getElementById</code>. This is exactly why ids exist — they are handles for both CSS and JavaScript.
A first taste of events
Interactivity comes from events — things the user does, like clicking. The simple <code>onclick</code> attribute above runs code on click. In real projects you separate JavaScript from HTML (you will learn that in the JS course), but this shows the core idea: an action triggers code that changes the page.
- 1Mark
Give the HTML element an id.
- 2Find
JavaScript locates it with getElementById.
- 3Change
Update its text, style or content.
- 4Trigger
An event like a click runs the code.
In the editable box, create a paragraph with an id and a button. Make the button change the paragraph's text when clicked. Then add a second button that changes it to something else.
1. Which element adds JavaScript to a page?
2. Where is it best to place a script?
3. How does JavaScript usually find an element?
4. What triggers interactivity?
- JavaScript is added with the <script> element (inline or external src).
- Place scripts before </body> so the HTML exists when they run.
- JavaScript connects to HTML through ids (getElementById).
- Events like clicks trigger code that changes the page.