Categories
JavaScript Answers

How to stop event propagation with inline onclick attribute with JavaScript?

Spread the love

To stop event propagation with inline onclick attribute with JavaScript, we can call the stopPropagation method.

For instance, we write

<div onclick="onClick(event);">click me</div>

to set the onclick attribute to call the onClick function with the click event object.

Then we write

function onClick(event) {
  event.stopPropagation();
}

to define the onClick function to call the stopPropagation method to stop the click event from bubbling up.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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