In JavaScript, an event is an action that happens in a web page. JavaScript can detect that action and execute code in response.
For example:
- Clicking a button
- Typing in an input field
- Moving the mouse
- Submitting a form
- Loading a webpage
- Pressing a keyboard key
Common JavaScript Events
Event Description
click - When an element is clicked
dblclick - When an element is double-clicked
mouseover - When the mouse moves over an element
mouseout - When the mouse leaves an element
keydown - When a keyboard key is pressed
keyup - When a keyboard key is released
input - When an input value changes
change - When a value is changed and committed
submit - When a form is submitted
load - When a page or resource finishes loading.
Example:
<!DOCTYPE html>
<h2 id="heading">Hello, JavaScript!</h2> <button>Click Me</button> <script> function showMessage() { document.getElementById("heading").innerText = "Button Clicked Successfully!"; } </script> 