When an event occurs, the browser creates an event object and passes it to the event handler, providing information about the event. Here are some important properties of the event object:
- type: The type of event that was triggered (e.g., "click", "keydown", "load").
- target: The element that triggered the event (i.e., the element that the event listener is attached to).
- currentTarget: The element that the event listener is currently being executed on (which may be different from target if the event is bubbling).
- bubbles: A boolean value indicating whether the event bubbles up through the DOM (true) or not (false).
- cancelable: A boolean value that indicates whether the event can be canceled (e.g., preventing the default action from happening).
- defaultPrevented: A boolean indicating whether preventDefault() was called on the event.
- eventPhase: Indicates which phase of event propagation is currently being executed (capturing, target, or bubbling).
- timeStamp: A timestamp (in milliseconds) of when the event was created.
- key (for keyboard events): The value of the key pressed, such as "a", "Enter", or "Escape".
- clientX / clientY: The horizontal and vertical coordinates of the mouse event relative to the browser window’s client area (not including scroll offset).
- screenX / screenY: The horizontal and vertical coordinates of the mouse event relative to the entire screen.
- altKey, shiftKey, ctrlKey, metaKey: Booleans that indicate whether the corresponding modifier keys (Alt, Shift, Control, or Meta) were pressed during the event.
- button (for mouse events): The button that was pressed on the mouse (e.g., left, middle, or right).
- keyCode / charCode (for older keyboard events): Numeric code representing the key pressed. keyCode is more common in older browsers, and charCode is used for characters.
These properties allow developers to get detailed information about the event and handle it appropriately in the event handler.
Related Next Post: Difference between target and currentTarget
No comments:
Post a Comment