Skip to main content

Normally, event handlers run during the bubbling phase. Notice what happens if you type something into the <input> in this example — the inner handler runs first, as the event ‘bubbles’ from the target up to the document, followed by the outer handler.

Sometimes, you want handlers to run during the capture phase instead. Add capture to the end of the event name:

App
<div onkeydowncapture={(e) => alert(`<div> ${e.key}`)} role="presentation">
	<input onkeydowncapture={(e) => alert(`<input> ${e.key}`)} />
</div>

Now, the relative order is reversed. If both capturing and non-capturing handlers exist for a given event, the capturing handlers will run first.

Edit this page on GitHub

1
2
3
4
<div onkeydown={(e) => alert(`<div> ${e.key}`)} role="presentation">
	<input onkeydown={(e) => alert(`<input> ${e.key}`)} />
</div>