Lesson 4 — Making it Interactive with JavaScript
HTML is structure. CSS is style. JavaScript is behaviour. It listens for what users do — clicks, typing, scrolling — and changes the page in response. No reload required.
JavaScript runs inside <script> tags at the bottom of your body. It can find any element on the page and change it. Here is the pattern you will use constantly:
// Find an element on the page
const btn = document.querySelector('.my-button');
// Define what should happen
function changeColor() {
document.body.style.backgroundColor = '#a8ff78';
document.body.style.color = '#0a0a06';
}
// Listen for a click and run the function
btn.addEventListener('click', changeColor);
const — a value that never changes. Use this by default.
let — a value that can change. Use when you need to update it.
document.querySelector() — finds the first matching element. Pass a CSS selector.
addEventListener() — listens for an event. Common events: click, input, submit.
[TRY IT NOW]
Add <button class="my-button">Click me</button> to your HTML. Add a <script> tag at the bottom of your body. Type the example above. Click the button. Watch the page change. That is JavaScript working.