๐ฏ Learning Objectives
- Understand JavaScript events
- Handle click events
- Use addEventListener()
- Work with keyboard and form events
- Build interactive applications
โก What are Events?
Events are actions that happen in the browser: clicks, keypresses, mouse movements, form submissions, etc. JavaScript can "listen" for these events and respond to them.
๐ฑ๏ธ Common Event Types
| Event | Description | Example |
|---|---|---|
| click | User clicks an element | Button click |
| dblclick | User double-clicks | Double-click image |
| mouseover | Mouse enters element | Hover effect |
| mouseout | Mouse leaves element | Remove hover |
| keydown | Key is pressed | Type in input |
| keyup | Key is released | After typing |
| submit | Form is submitted | Submit button clicked |
| change | Input value changes | Select dropdown |
๐ Click Events
Method 1: Inline onclick
<button onclick="alert('Button clicked!')">Click Me</button>
<button onclick="changeText()">Change Text</button>
<script>
function changeText() {
document.getElementById("demo").textContent = "Text changed!";
}
</script>
Method 2: addEventListener() (Recommended)
// HTML
<button id="myButton">Click Me</button>
// JavaScript
let button = document.getElementById("myButton");
button.addEventListener("click", function() {
alert("Button was clicked!");
});
๐จ Practical Examples
1. Counter App
Counter App
0
2. Color Changer
<div id="box" style="width: 200px; height: 200px; background: lightblue;"></div>
<button id="changeColor">Change Color</button>
<script>
let box = document.getElementById("box");
let button = document.getElementById("changeColor");
button.addEventListener("click", function() {
let colors = ["lightcoral", "lightgreen", "lightyellow", "lightpink"];
let randomColor = colors[Math.floor(Math.random() * colors.length)];
box.style.backgroundColor = randomColor;
});
</script>
โจ๏ธ Keyboard Events
<input type="text" id="nameInput" placeholder="Type your name">
<p id="output"></p>
<script>
let input = document.getElementById("nameInput");
let output = document.getElementById("output");
input.addEventListener("keyup", function() {
output.textContent = "Hello, " + input.value;
});
</script>
๐ฑ๏ธ Mouse Events
<div id="hoverBox" style="padding: 50px; background: lightblue;">
Hover over me!
</div>
<script>
let hoverBox = document.getElementById("hoverBox");
hoverBox.addEventListener("mouseover", function() {
hoverBox.style.backgroundColor = "lightcoral";
hoverBox.textContent = "Mouse is over!";
});
hoverBox.addEventListener("mouseout", function() {
hoverBox.style.backgroundColor = "lightblue";
hoverBox.textContent = "Hover over me!";
});
</script>
๐งช Live Demo on This Page
Interactive Counter
0
๐ป Complete Practice Example
JavaScript Events Practice
Click Event
Click the button!
Toggle Visibility
This text can be hidden
Live Input
You typed:
Color Changer
Click buttons to change background
โ๏ธ Practice Exercises
- Create a button that changes its own text when clicked
- Build a light/dark mode toggle button
- Create an input that displays character count as you type
- Make a div that changes color on mouseover and mouseout
- Build a simple calculator with click events
โ Today's Deliverable
- โ events-practice.html with interactive features
- โ Counter app with increment/decrement/reset
- โ At least 3 different event types used
- โ addEventListener() implemented
- โ Click, keyboard, and mouse events
- โ Toggle visibility feature
- โ Working demo screenshots