An Event is an action or occurrence that happens in a web browser.
Events can be generated by:
- Clicking a button
- Moving the mouse over an element
- Moving the mouse out of an element
- Pressing a keyboard key
- Submitting a form
- Changing the value of an input field
- Loading a webpage
JavaScript allows us to respond to these events by executing specific functions.
An Event Handler is a JavaScript function that is executed automatically when a particular event occurs.
Example:
<button onclick="fun()">Click Me</button>Here,
onclick→ Eventfun()→ Event Handler Function
Common.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Events in JavaScript</title>
<script src="events.js"></script>
</head>
<body>
<button onclick="fun()">Message</button>
<button onclick="enjoy()">Information</button>
</body>
</html>events.js
function fun(){
alert("Welcome to JavaScript Alert");
}
function enjoy(){
confirm("Welcome to Confirm");
}When the user clicks:
alert("Welcome to JavaScript Alert");Output:
Welcome to JavaScript Alert
A popup alert box appears.
confirm("Welcome to Confirm");Output:
Welcome to Confirm
OK Cancel
A confirmation box appears with:
- OK Button
- Cancel Button
The alert() method is used to display a warning or informational message.
alert("Message");alert("Welcome to JavaScript");The confirm() method displays a confirmation dialog box.
confirm("Are you sure?");Are you sure?
OK Cancel
It returns:
- true → If OK is clicked
- false → If Cancel is clicked
<!DOCTYPE html>
<html lang="en">
<head>
<title>Event</title>
<script src="events.js"></script>
</head>
<body>
<button onclick="fun1()">OnClick</button>
<button onmouseover="fun2()">OnMouseOver</button>
<button onmouseout="fun3()">OnMouseOut</button>
</body>
</html>function fun1(){
alert("Welcome to function1");
}
function fun2(){
alert("Welcome to function2");
}
function fun3(){
alert("Welcome to function3");
}When the button is clicked:
Welcome to function1
When the mouse pointer moves over the button:
Welcome to function2
When the mouse pointer leaves the button:
Welcome to function3
| Event | Description |
|---|---|
| onclick | Triggered when an element is clicked |
| ondblclick | Triggered when double-clicked |
| onmouseover | Triggered when mouse moves over an element |
| onmouseout | Triggered when mouse leaves an element |
| onkeydown | Triggered when a key is pressed |
| onkeyup | Triggered when a key is released |
| onchange | Triggered when input value changes |
| onsubmit | Triggered when a form is submitted |
| onfocus | Triggered when an input gets focus |
| onblur | Triggered when an input loses focus |
| onload | Triggered when page finishes loading |
Apart from inline events, JavaScript provides:
addEventListener()This is the modern way of handling events.
element.addEventListener("event", functionName);<button id="btn">Click Me</button>document.getElementById("btn")
.addEventListener("click", message);
function message(){
alert("Button Clicked");
}| Inline Event | addEventListener() |
|---|---|
| Written inside HTML | Written in JavaScript |
| Less maintainable | More maintainable |
| One event at a time | Multiple events can be attached |
| Old approach | Modern approach |
- Events are actions performed by the user or browser.
- Event Handlers are functions executed when an event occurs.
onclickis triggered when an element is clicked.onmouseoveris triggered when mouse enters an element.onmouseoutis triggered when mouse leaves an element.alert()displays a popup message.confirm()displays OK and Cancel buttons.addEventListener()is the recommended modern way to handle events.
Events are one of the most important features of JavaScript. They allow developers to create interactive web pages by responding to user actions such as clicks, mouse movements, keyboard actions, and form submissions. Understanding events is essential for DOM Manipulation, Form Validation, and building dynamic web applications using modern JavaScript frameworks.