العودة إلى الدورة

The Event Object and Event Properties

JavaScript: الدورة الكاملة للمبتدئين من 'الصفر إلى الاحتراف'

90. The Event Object

When an event occurs, the browser creates an Event Object and passes it as the first argument to the handler function. This object contains crucial information about the event that just happened.

Accessing the Event Object

We typically name the event object e or event.

javascript const input = document.getElementById('user-input');

input.addEventListener('keydown', (e) => { // The 'e' here is the event object console.log('Key pressed:', e.key); // Which key was pressed (e.g., 'A', 'Enter') console.log('Target element:', e.target); // The element that triggered the event });

Key Event Object Methods

  1. e.preventDefault(): Stops the browser's default behavior for a specific event (e.g., stopping a form from submitting when validating input, or preventing a link from navigating).
  2. e.stopPropagation(): Prevents the event from bubbling up to parent elements (advanced topic: Event Propagation).