Skip to content

preventDefault( ) vs stopPropagation

  • by

preventDefault() and stopPropagation() are two different methods used in event handling in JavaScript. Here’s what they do:

1. preventDefault(): This method is used to prevent the default behavior of an event. In web development, many HTML elements have default behaviors associated with certain events.

For example, when you click on a link (<a> element), the browser navigates to the URL specified in the href attribute. If you want to override this default behavior and perform a different action when the link is clicked, you can call preventDefault() on the event object passed to the event handler function.

document.querySelector('a').addEventListener('click', function(event) {
  event.preventDefault();
  // Perform custom action instead of following the link
});

2. stopPropagation(): This method is used to stop the propagation of an event through the DOM (Document Object Model) tree. When an event occurs on an element, it typically propagates through its parent elements, triggering their event handlers as well. By calling stopPropagation() on the event object, you can prevent the event from being propagated further up the DOM tree.

document.querySelector('.inner').addEventListener('click', function(event) {
  event.stopPropagation();
  // This event handler will be triggered,
  // but the event won't propagate to the outer element
});

document.querySelector('.outer').addEventListener('click', function() {
  // This event handler won't be triggered
});

Here’s a tabular comparison between preventDefault() and stopPropagation():

preventDefault()stopPropagation()
PurposePrevents the default behavior of an eventStops the propagation of an event through the DOM tree
EffectOverrides the default behavior of an eventPrevents the event from reaching parent elements
UsageUsed on the event object in event handlersUsed on the event object in event handlers
Event Behavior AffectedDefault behavior associated with the eventEvent propagation to parent elements
Applicable EventsApplicable to events that have default behaviorApplicable to any event that propagates through the DOM tree
Multiple Event HandlersCalling preventDefault() in one handler does not affect other event handlers for the same eventCalling stopPropagation() in one handler prevents the event from reaching other event handlers in parent elements
CompatibilityCompatible with most modern browsersCompatible with most modern browsers

preventDefault( ) vs stopPropagation example

Here’s an example that demonstrates the usage of both preventDefault() and stopPropagation() in event handling:

<!DOCTYPE html>
<html>
<head>
  <title>Event Handling Example</title>
</head>
<body>
  <div class="outer">
    <div class="inner">
      <button>Click Me</button>
    </div>
  </div>

  <script>
    document.querySelector('.inner').addEventListener('click', function(event) {
      event.stopPropagation();
      console.log("Inner element clicked");
    });

    document.querySelector('.outer').addEventListener('click', function() {
      console.log("Outer element clicked");
    });

    document.querySelector('button').addEventListener('click', function(event) {
      event.preventDefault();
      console.log("Button clicked");
    });
  </script>
</body>
</html>

Output:

preventDefault( ) vs stopPropagation JavaScript

In this example, we have an outer <div> element with a class of “outer” and an inner <div> element with a class of “inner”. Inside the inner <div>, there is a <button> element.

When you click the button, the following events will occur:

  1. The button click event handler will be triggered first. It calls event.preventDefault() to prevent the default behavior of the button, which is submitting a form or navigating to a new page.
  2. Then, the button click event propagates to the inner <div> element. The inner event handler is triggered, and it calls event.stopPropagation(). This stops the event from propagating further to the outer <div> element.
  3. As a result, the event does not reach the outer event handler, and “Outer element clicked” is not logged to the console.

Comment if you have any doubts or suggestions on this Js event topic.

Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.

OS: Windows 10

Code: HTML 5 Version

Leave a Reply

Your email address will not be published. Required fields are marked *