Skip to content
Advertisement

JS: using capturing with onclick

I know when using addEventListener, you can add in another argument of true to tell it to not bubble and to use capturing instead. Can this you tell onclick to use capture as well when using it instead of addEventListener?

Advertisement

Answer

Properties of elements like onclick are called IDL attributes. See the specification. When its setter is invoked, it adds the assigned function to the event handler map of the element. That precise name has a very specific meaning here: “event handlers” are:

Many objects can have event handlers specified. These act as non-capture event listeners for the object on which they are specified.

They always listen in the bubbling phase, never in the capturing phase. So, what you’re looking for isn’t possible.

In contrast, addEventListener adds an event listener, which has a slightly different official definition and can be either capturing or bubbling.

If you want to add a listener for the capturing phase, you must use addEventListener.

Advertisement