Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS: using capturing with onclick

Tags:

javascript

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?

like image 545
MarketMan39 Avatar asked Oct 29 '25 02:10

MarketMan39


1 Answers

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.

like image 113
CertainPerformance Avatar answered Oct 30 '25 18:10

CertainPerformance



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!