๐Ÿ“… Week 7 - Day 2: JavaScript Events

Wednesday | Duration: 3 hours

๐ŸŽฏ Learning Objectives

โšก 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

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

Event Practice

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

  1. Create a button that changes its own text when clicked
  2. Build a light/dark mode toggle button
  3. Create an input that displays character count as you type
  4. Make a div that changes color on mouseover and mouseout
  5. Build a simple calculator with click events

โœ… Today's Deliverable

โ† Previous Course Home Next: Day 3 โ†’