The HTML oncuechange event
The HTML oncuechange attribute runs JavaScript when a text track's active cues change (e.g. captions). It is an inline handler for the cuechange event; in modern code prefer addEventListener('cuechange', …).
Overview
The oncuechange event attribute runs JavaScript when a media text track's cues change. In JavaScript the event itself is named cuechange. Drop the on prefix when you call addEventListener.
It is a text-track event, fired by <track> elements and by the TextTrack objects reached through video.textTracks, whenever the set of currently active cues changes. It is the hook for rendering your own captions, chapters or subtitle UI.
You can wire this up with the inline oncuechange HTML attribute, but the modern, recommended approach is element.addEventListener('cuechange', 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 oncuechange="handler()">…</element>
element.addEventListener('cuechange', handler);
Best practices
- Prefer
element.addEventListener('cuechange', handler)over the inlineoncuechangeattribute; it separates behavior from markup and allows multiple handlers. - Attach media events to the <video>/<audio> element to build custom player UI.
- Read the element's state (
currentTime,duration,buffered) inside the handler. - Still provide native
controlsand captions for accessibility.
Frequently asked questions
What is the oncuechange event?
cuechange.Which elements fire this event?
How do I read the active cues?
activeCues on the text track (for example video.textTracks[0].activeCues), a TextTrackCueList of the cues showing right now.Should I use the oncuechange attribute or addEventListener?
element.addEventListener('cuechange', …) in JavaScript. The inline oncuechange attribute works but mixes behavior into the markup and allows only one handler per element.