CodeGym /Courses /Frontend SELF EN /Properties and Methods of the Event Object

Properties and Methods of the Event Object

Frontend SELF EN
Level 42 , Lesson 0
Available

6.1 Event Object

JavaScript provides a powerful event system that lets you keep track of various user actions and other events happening in the browser. A key part of working with events is the event object, which contains info about the event and provides methods for handling it.

When an event occurs, the browser creates an event object that contains info about the event. This object is passed to the event handler as an argument.

Event Object Properties:

  1. type: the type of the event (e.g., click, keydown).
  2. target: the element where the event occurred.
  3. currentTarget: the element to which the event handler is attached.
  4. eventPhase: the phase in which the event is at (capturing, target, bubbling).
  5. bubbles: a boolean indicating if the event can bubble up.
  6. cancelable: a boolean indicating if the event can be canceled.
  7. defaultPrevented: a boolean indicating if the default action has been prevented.
  8. timeStamp: the time when the event was created.
  9. isTrusted: a boolean indicating if the event was triggered by a user or a script.

Event Object Methods:

  1. preventDefault(): cancels the default action associated with the event.
  2. stopPropagation(): stops further event propagation.
  3. stopImmediatePropagation(): stops further event propagation and prevents other event handlers from being executed on the current element.

Example of Using the Event Object:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Event Object Example</title>
        </head>
        <body>
          <button id="myButton">Click me</button>

          <script>
            const button = document.getElementById('myButton');

            button.addEventListener('click', function(event) {
              console.log('Event type:', event.type); // Output: click
              console.log('Event target:', event.target); // Output: <button id="myButton">Click me</button>
              console.log('Current target:', event.currentTarget); // Output: <button id="myButton">Click me</button>
              console.log('Event phase:', event.eventPhase); // Output: 2 (Target phase)
              console.log('Bubbles:', event.bubbles); // Output: true
              console.log('Cancelable:', event.cancelable); // Output: true
              console.log('Time stamp:', event.timeStamp); // Output time in milliseconds
              console.log('Is trusted:', event.isTrusted); // Output: true
            });
          </script>
        </body>
      </html>
    
  

6.2 Click Events (Mouse Events)

Mouse events are generated when a user interacts with an element using a mouse:

  • click: click event
  • dblclick: double-click event
  • mousedown: mouse button down event
  • mouseup: mouse button up event
  • mousemove: mouse movement event
  • mouseover: mouse hover event
  • mouseout: mouse out event

Example:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Mouse Events Example</title>
        </head>
        <body>
          <button id="myButton">Click Me</button>
          <script>
            const button = document.getElementById('myButton');

            button.addEventListener('click', function(event) {
              console.log('Button clicked');
              console.log('Event type:', event.type);
              console.log('Target element:', event.target);
            });
          </script>
        </body>
      </html>
    
  

6.3 Load Events

Load events occur when a resource finishes loading:

  • load: event that occurs when a resource/page is fully loaded
  • DOMContentLoaded: event that occurs when the initial HTML document is loaded and parsed without waiting for stylesheets, images, and subframes to finish loading

Example:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Load Events Example</title>
        </head>
        <body>
          <script>
            window.addEventListener('load', function(event) {
              console.log('Window fully loaded');
            });

            document.addEventListener('DOMContentLoaded', function(event) {
              console.log('DOM fully loaded and parsed');
            });
          </script>
        </body>
      </html>
    
  

6.4 Focus Events

Focus events are generated when an element gains or loses focus.

  • focus: event that occurs when an element gains focus
  • blur: event that occurs when an element loses focus

Example:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Focus Events Example</title>
        </head>
        <body>
          <input type="text" id="textInput" placeholder="Type something...">
          <script>
            const input = document.getElementById('textInput');

            input.addEventListener('focus', function(event) {
              console.log('Input field focused');
            });

            input.addEventListener('blur', function(event) {
              console.log('Input field lost focus');
            });
          </script>
        </body>
      </html>
    
  

6.5 Keyboard Events

Keyboard events are generated when a user presses keys on the keyboard:

  • keydown: event that occurs when a key is pressed
  • keyup: event that occurs when a key is released
  • keypress: event that occurs when a key is pressed and released (deprecated, not recommended for use)

Example:

HTML
    
      <html>
        <head>
          <title>Keyboard Events Example</title>
        </head>
        <body>
          <input type="text" id="textInput" placeholder="Type something...">
          <script>
            const input = document.getElementById('textInput');

            input.addEventListener('keydown', function(event) {
              console.log('Key down:', event.key);
            });

            input.addEventListener('keyup', function(event) {
              console.log('Key up:', event.key);
            });
          </script>
        </body>
      </html>
    
  
1
Task
Frontend SELF EN, level 42, lesson 0
Locked
Mouse Coordinates
Mouse Coordinates
1
Task
Frontend SELF EN, level 42, lesson 0
Locked
Loading Events
Loading Events
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION