Exploring 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
index.html
style.css
Danke für Ihr Feedback!
Fragen Sie AI
Fragen Sie AI
Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen
Can you explain the difference between `target` and `currentTarget` with an example?
What are some common use cases for accessing event object properties in JavaScript?
Can you list more useful properties or methods available on the event object?
Awesome!
Completion rate improved to 6.25
Exploring the Event Object
Swipe um das Menü anzuzeigen
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
index.html
style.css
Danke für Ihr Feedback!