Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Exploring the Event Object | Event Objects and Event Flow
Event Handling and User Interaction in JavaScript

bookExploring the Event Object

When you interact with a web page—such as clicking a button or pressing a key—the browser creates an event object. This object contains detailed information about the event that just occurred, allowing you to respond intelligently in your JavaScript code. Every event handler function receives this event object as its first argument, which gives you access to useful properties and methods.

Two of the most important properties are target and currentTarget. The target property refers to the actual element that triggered the event, while currentTarget refers to the element that the event handler is currently attached to. This distinction is crucial when you attach a single event handler to a parent element and want to know which child element was interacted with. Another useful property is type, which tells you what kind of event occurred, such as "click", "keydown", or "mouseover".

For mouse events, the event object provides properties like clientX and clientY (the mouse coordinates), button (which mouse button was pressed), and more. For keyboard events, you can use key (the character or key pressed), code (the physical key on the keyboard), and altKey, ctrlKey, shiftKey, or metaKey (to check if modifier keys were held down).

Understanding these properties helps you write flexible and robust event handlers that can adapt to different user interactions.

script.js

script.js

index.html

index.html

style.css

style.css

copy
question mark

Which statement best describes the difference between event.target and event.currentTarget in a click event handler attached to a list?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookExploring the Event Object

Sveip for å vise menyen

When you interact with a web page—such as clicking a button or pressing a key—the browser creates an event object. This object contains detailed information about the event that just occurred, allowing you to respond intelligently in your JavaScript code. Every event handler function receives this event object as its first argument, which gives you access to useful properties and methods.

Two of the most important properties are target and currentTarget. The target property refers to the actual element that triggered the event, while currentTarget refers to the element that the event handler is currently attached to. This distinction is crucial when you attach a single event handler to a parent element and want to know which child element was interacted with. Another useful property is type, which tells you what kind of event occurred, such as "click", "keydown", or "mouseover".

For mouse events, the event object provides properties like clientX and clientY (the mouse coordinates), button (which mouse button was pressed), and more. For keyboard events, you can use key (the character or key pressed), code (the physical key on the keyboard), and altKey, ctrlKey, shiftKey, or metaKey (to check if modifier keys were held down).

Understanding these properties helps you write flexible and robust event handlers that can adapt to different user interactions.

script.js

script.js

index.html

index.html

style.css

style.css

copy
question mark

Which statement best describes the difference between event.target and event.currentTarget in a click event handler attached to a list?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt