JavaScript-primer

Event-Driven Programming

Event-driven programming is a programming paradigm in which the flow of a program is determined by events that occur during the execution of the program. In an event-driven program, the program waits for a specific event to occur, such as a user clicking a button, and then executes a predetermined action in response to the event.

Event-driven programming is commonly used in graphical user interface (GUI) programming, where the program responds to user input events such as mouse clicks, key presses, and touch gestures. It is also used in network programming, where the program responds to network events such as incoming connections or data.

In JavaScript, event-driven programming is implemented using event listeners, which are functions that are registered to be called in response to a specific event. For example, an event listener can be added to a button element to execute a function when the button is clicked:

<!-- HTML document -->
<button id="my-button">Click me</button>

<script>
  // Retrieve the button element
  const button = document.getElementById('my-button');

  // Add an event listener to the button
  button.addEventListener('click', () => {
    // Execute this function when the button is clicked
    console.log('Button clicked');
  });
</script>

In this example, the addEventListener() method is used to add an event listener to the button element that listens for the click event. When the button is clicked, the event listener function is executed, which logs a message to the console.

Event-driven programming is a powerful way to build interactive and responsive programs, and is an important concept in modern programming languages and frameworks.


In JavaScript, the addEventListener() method is used to register an event listener on an HTML or XML element. The addEventListener() method takes three arguments: the event type, the event listener function, and an options object. The options object can contain three optional properties: capture, once, and passive.

Here is an example of using addEventListener() with the options object:

<!-- HTML document -->
<button id="my-button">Click me</button>

<script>
  // Retrieve the button element
  const button = document.getElementById('my-button');

  // Add an event listener to the button
  button.addEventListener('click', () => {
    // Execute this function when the button is clicked
    console.log('Button clicked');
  }, {
    // Set the options object
    capture: true, // Capture the event in the capturing phase
    once: true, // Remove the event listener after it is called
    passive: true // Indicate that the event listener is passive (doesn't call preventDefault)
  });
</script>

In this example, the event listener is registered to execute the function when the button element is clicked. The options object is set with the capture, once, and passive properties, which have the following effects:

capture: If set to true, the event listener is registered in the capturing phase of the event propagation, which means it is called before the event reaches the target element. If set to false (the default), the event listener is registered in the bubbling phase, which means it is called after the event reaches the target element.

once: If set to true, the event listener is removed after it is called for the first time. If set to false (the default), the event listener remains registered and can be called multiple times.

passive: If set to true, the event listener is marked as passive, which means it does not call the preventDefault() method on the event object. If set to false (the default), the event listener can call preventDefault() if necessary.

These options can be useful in optimizing the performance of event listeners and fine-tuning the behavior of event-driven programs.

Note: The signal property is not a part of the addEventListener() options object. The signal property is a part of the AbortSignal interface, which is used to provide a way to abort asynchronous tasks in JavaScript.


Event Propagation

Event propagation, also known as event bubbling, is the process by which events are propagated from the target element to the ancestor elements in the DOM (Document Object Model) tree. In JavaScript, event propagation consists of two phases: the capturing phase and the bubbling phase.

During the capturing phase, the event travels from the root element of the document (the window object) down to the target element. During the bubbling phase, the event travels from the target element back up to the root element.

Event propagation is useful for allowing multiple event listeners to be registered on different elements in the DOM tree, and for enabling events to be handled at different levels of the tree. For example, an event listener can be registered on the root element to handle all events that occur in the document, or on a specific element to handle only events that occur within that element.

Here is an example of event propagation:

<!-- HTML document -->
<style>
  #my-div {
    background-color: red;
    width: 200px;
    height: 100px;
  }
</style>
<div id="my-div">
  <button id="my-button">Click me</button>
</div>

<script>
  // Retrieve the button element
  const button = document.getElementById('my-button');

  // Add an event listener to the button in the capturing phase
  button.addEventListener('click', () => {
    console.log('Button clicked (capturing)');
  }, { capture: true });

  // Add an event listener to the div element in the bubbling phase
  const div = document.getElementById('my-div');
  div.addEventListener('click', () => {
    console.log('Button clicked (bubbling)');
  });
</script>


In this example, two event listeners are registered on the button and div elements. The first event listener is registered on the button element in the capturing phase, and the second event listener is registered on the div element in the bubbling phase.

When the button element is clicked, the event is first propagated through the capturing phase, and the event listener registered on the button element is called. The event then propagates through the bubbling phase, and the event listener registered on the div element is called. The result is that both event listeners are called in the order that they were registered.

Event propagation allows multiple event listeners to be registered on different elements in the DOM tree, and enables events to be handled at different levels of the tree. It is an important concept in event-driven programming and is used in many modern programming languages and frameworks.


Event Delegation

Event delegation is a technique in JavaScript that involves using a single event listener on a parent element to handle events that are triggered by its child elements. This technique can be useful for optimizing the performance of event-driven programs, particularly when dealing with large or dynamic sets of elements that require event handling.

In event delegation, the parent element is used as a “delegate” for the events that are triggered by its child elements. The event listener is registered on the parent element, and the event handling logic is implemented in the event listener function. When an event is triggered by a child element, the event listener function is called and can determine the target element that triggered the event based on the event object.

Here is an example of using event delegation in JavaScript:

<!-- HTML document -->
<style>
  .my-item {
    background-color: red;
    width: 200px;
    height: 100px;
  }
</style>
<ul id="my-list">
  <li class="my-item">Item 1</li>
  <li class="my-item">Item 2</li>
  <li class="my-item">Item 3</li>
</ul>

<script>
  // Retrieve the list element
  const list = document.getElementById('my-list');

  // Add an event listener to the list element
  list.addEventListener('click', (event) => {
    // Check if the event target is an item element
    if (event.target.matches('.my-item')) {
      console.log('Item clicked');
    }
  });
</script>

In this example, an event listener is registered on the list element using the addEventListener() method. The event listener function is called when any element within the list element is clicked, and the event object is passed to the function as an argument. The function uses the matches() method to check if the event target (the element that was clicked) is a li element with the class my-item, and if it is, it logs a message to the console.

Event delegation allows a single event listener to handle events triggered by multiple elements, and can be a useful technique for optimizing the performance of event-driven programs. It is often used in combination with other DOM manipulation methods and technologies such as HTML, CSS, and JavaScript libraries or frameworks.