The HTML ontransitionend event
The HTML ontransitionend attribute runs JavaScript when a CSS transition completes. It is an inline handler for the transitionend event; in modern code prefer addEventListener('transitionend', …).
Overview
The ontransitionend event attribute runs JavaScript when a CSS transition finishes. In JavaScript the event itself is named transitionend — drop the on prefix when you call addEventListener.
It is a CSS transition event. The handler receives a TransitionEvent with the propertyName that transitioned, which is the clean way to chain an action after a CSS transition completes.
You can wire this up with the inline ontransitionend HTML attribute, but the modern, recommended approach is element.addEventListener('transitionend', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.
Syntax
<element ontransitionend="handler()">…</element>
element.addEventListener('transitionend', handler);
Best practices
- Prefer
element.addEventListener('transitionend', handler)over the inlineontransitionendattribute — it separates behavior from markup and allows multiple handlers. - Use it to run code after a CSS transition instead of guessing with a timer.
- Check
event.propertyNamewhen several properties transition at once. - Respect
prefers-reduced-motionfor the transitions these events track.
Frequently asked questions
What is the ontransitionend event?
transitionend.How do I run code after a CSS transition ends?
Which property finished transitioning?
event.propertyName from the TransitionEvent.Should I use the ontransitionend attribute or addEventListener?
addEventListener('transitionend', …) in JavaScript. The inline ontransitionend attribute works but mixes behavior into the markup and allows only one handler per element.