Lesson 47 - Different Types of JavaScript Events
JavaScript provides a wide variety of events that allow you to interact with web pages dynamically. These events can be triggered by user actions such as clicking, typing, scrolling, submitting a form, or even moving the mouse.
Types of Events
1. Mouse Events (Triggered by mouse interactions)
click
-> When an element is clickeddblclick
-> When an element is double-clickedmousedown
-> When the mouse button is pressedmouseup
-> When the mouse button is releasedmousemove
-> When the mouse moves over an elementmouseenter
-> When the mouse enters an elementmouseleave
-> When the mouse leaves an element
2. Keyboard Events (Triggered when a user interacts with the keyboard)
keydown
-> When a key is pressed downkeyup
-> When a key is releasedkeypress
-> (Deprecated) When a key is pressed
3. Form Events (Triggered when interacting with form elements)
submit
-> When a form is submittedchange
-> When the value of an input changesinput
-> When an input field receives inputfocus
-> When an input field gains focusblur
-> When an input field loses focus
4. Window Events (Triggered by actions on the browser window)
resize
-> When the window is resizedscroll
-> When the user scrollsload
-> When the page is fully loadedunload
-> When the user leaves the page
Example 1: Click Event
document.getElementById("myButton").addEventListener("click", function () {
alert("Button Clicked!");
});
- This triggers an alert when the button is clicked.
Example 2: Keydown Event
document.addEventListener("keydown", function (event) {
console.log("Key Pressed:", event.key);
});
- This logs the key pressed in the browser console.
Example 3: Form Submit Event
document.getElementById("myForm").addEventListener("submit", function (event) {
event.preventDefault(); // Prevents form submission
alert("Form Submitted!");
});
- This prevents the form from submitting and shows an alert.
Try Your Hand
Steps:
-
Create a File:
- Name it
lesson47-events.html
.
- Name it
-
Write the Code:
-
Copy the following:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>JavaScript Events</title> </head> <body> <button id="clickMe">Click Me</button> <input type="text" id="textInput" placeholder="Type something"> <form id="form"> <input type="text" placeholder="Enter Name"> <button type="submit">Submit</button> </form> <script> document.getElementById("clickMe").addEventListener("click", function () { alert("Button Clicked!"); }); document.getElementById("textInput").addEventListener("keydown", function (event) { console.log("Key Pressed:", event.key); }); document.getElementById("form").addEventListener("submit", function (event) { event.preventDefault(); alert("Form Submitted!"); }); </script> </body> </html>
-
-
Save the File.
-
Open it in a Browser to See the Different Event Actions.
Key Takeaways
- JavaScript events let you interact with users dynamically.
- Events include mouse clicks, keyboard presses, form submissions, and window changes.
- The
addEventListener
method allows handling multiple events efficiently.