Introduction
Web pages often feel static and boring without interaction. Making buttons clickable, forms respond, or images change when you hover adds life to websites. JavaScript is the tool that brings these actions to life on web pages.
Imagine a puppet show where the puppeteer controls the puppets to move and talk when the audience claps or shouts. JavaScript is like the puppeteer, watching for audience actions and making the puppets respond instantly.
┌───────────────┐ listens for ┌───────────────┐
│ User Action │────────────────────────▶│ Event Listener│
└───────────────┘ └──────┬────────┘
│ runs code
▼
┌─────────────────┐
│ JavaScript Code │
└────────┬────────┘
│ changes
▼
┌─────────────────┐
│ Web Page (DOM) │
└─────────────────┘const button = document.querySelector('button'); button.addEventListener('click', () => { const message = document.querySelector('#message'); message.textContent = 'You clicked the button!'; });