Chapter 7: Events in JavaScript

JavaScript Lecture 7 Notes

Events in JS

The change in the state of an object is known as an Event.

Events are fired to notify code of "interesting changes".

Types of Events

Event Reference MDN Inline Event Handling Example

Event Handling in JS

Event handling is used to perform actions when an event occurs.

node.event = () => {
//handle here
}

btn.onclick = () => {
console.log("btn was clicked");
}
Event Handling in JS

Event Object

It is a special object that has details about the event.

All event handlers have access to the Event Object's properties and methods.

node.event = (e) => {
//handle here
}

Common Properties

Event Object

Event Listeners

Event listeners are used to attach multiple event handlers.

node.addEventListener( event, callback )

node.removeEventListener( event, callback )

Note: callback reference same होना चाहिए remove के लिए

Event Listeners

Lets Practice

Qs. Create a toggle button for dark/light mode.

Solution

Homework