HTML oncuechange Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The oncuechange attribute is an event-handler attribute for the <track> element. It runs JavaScript when the cuechange event fires — the text track’s currently displaying cues changed (a WebVTT line started or ended).

Put <track> inside <video> or <audio>. Read active lines from event.target.track.activeCues. The same event also fires on the underlying TextTrack object.

Baseline · Widely available.

Not deprecated and not experimental. cuechange on HTMLTrackElement is widely available since about July 2019 (the TextTrack event earlier).

Track must not be disabled.

Cues only activate when the track mode is showing or hidden. Use the default attribute on <track>, or set textTrack.mode in script.

HTML Syntax

syntax.html
<track oncuechange=" script">

script is JavaScript that runs when active cues change. Equivalent JS: trackEl.oncuechange = handler or trackEl.addEventListener("cuechange", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when active cues changeTry it
oncuechange="fn()"Call a named function from the attribute on <track>Try it
activeCuesRead current cue text via event.target.track.activeCuesTry it
addEventListener("cuechange")Preferred modern listener on the track element or TextTrackTry it
Custom caption panelUse mode hidden + cuechange to render your own UITry it

Live Preview

Press play. When WebVTT cues change, the status line updates. Enable captions in the player if needed.

Media

Sample /video/count.mp4 + /video/count.vtt. Track uses default.

Video + captions

Waiting for cuechange…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline oncuechange on track

Update a status line whenever active cues change.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
  <track
    kind="captions"
    src="/video/count.vtt"
    srclang="en"
    label="English"
    default
    oncuechange="showCue(event)">
</video>
<p id="status">Waiting for cuechange…</p>

<script>
  function showCue(event) {
    var cues = event.target.track.activeCues;
    document.getElementById("status").textContent =
      cues && cues.length ? cues[0].text : "No active cue";
  }
</script>
Try It Yourself

How It Works

  1. <track> loads a WebVTT file of timed captions.
  2. default enables the track so cues can become active.
  3. Playback advances — when a cue starts or ends, cuechange fires.
  4. oncuechange reads activeCues and shows the current line.

Example 2 — addEventListener and activeCues

Preferred script pattern on the track element.

HTML
<audio id="clip" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
  <track id="caps" kind="captions" src="/audio/count.vtt" srclang="en" label="English" default>
</audio>
<p id="panel">Waiting for cues…</p>

<script>
  document.getElementById("caps").addEventListener("cuechange", function (event) {
    var cues = event.target.track.activeCues;
    document.getElementById("panel").textContent =
      cues && cues.length ? cues[0].text : "No active cue";
  });
</script>
Try It Yourself

How It Works

  1. Select the <track> by id.
  2. Listen for cuechange with addEventListener.
  3. event.target.track is the TextTrack for that element.
  4. activeCues[0].text is the current caption string (if any).

Example 3 — Custom Caption Panel (mode hidden)

Keep native captions off-screen and draw your own caption line.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
  <track id="caps" kind="captions" src="/video/count.vtt" srclang="en" label="English">
</video>
<p id="custom" aria-live="polite">—</p>

<script>
  var video = document.getElementById("clip");
  var caps = document.getElementById("caps");
  var custom = document.getElementById("custom");

  video.addEventListener("loadedmetadata", function () {
    caps.track.mode = "hidden";
  });

  caps.addEventListener("cuechange", function () {
    var cues = caps.track.activeCues;
    custom.textContent = cues && cues.length ? cues[0].text : "—";
  });
</script>
Try It Yourself

How It Works

  1. Set mode = "hidden" so cues still activate without the browser overlay.
  2. Wait for metadata so the TextTrack is ready.
  3. On cuechange, copy activeCues text into your own element.
  4. Use aria-live if screen-reader users should hear caption updates.

Edge Cases & Pitfalls

Most failures are a disabled track, missing VTT, or listening on the wrong object.

mode

Disabled tracks never cuechange

If mode is disabled, cues stay inactive. Use default, or set showing / hidden.

origin

Same-origin VTT (unless CORS)

Track src must match the document origin unless the media element uses a proper crossorigin setup.

vs timeupdate

Not the same as ontimeupdate

timeupdate fires often during playback. Prefer cuechange when you only care about caption line changes.

target

Listen on track or TextTrack

The event fires on both. On the element, use event.target.track.activeCues. On TextTrack, use track.activeCues directly.

empty

Empty activeCues is normal

Between cues (or after the last one), the list can be empty. Clear your custom caption UI in that case.

Browser Support

oncuechange is an HTML event-handler attribute for media text tracks in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

oncuechange

Widely available on HTMLTrackElement since about July 2019. Chrome, Firefox, Safari, and Edge support cuechange for WebVTT tracks on audio and video.

95% Global support (approx.)
Google Chrome 23+
Full support
Mozilla Firefox 31+
Full support
Apple Safari 6+
Full support
Microsoft Edge 12+
Full support
Opera 15+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer 10+
Supported
oncuechange Widely supported

Bottom line: Safe to use for caption-driven UI. Ensure the track is enabled (default or mode), and prefer addEventListener in production.

Key Takeaways

  • Role: run script when a text track’s active cues change.
  • Element: <track> inside audio/video + WebVTT.
  • Data: read activeCues for the current caption text.
  • Enable track: default or mode showing/hidden.

One line: oncuechange updates when caption cues change — enable the track, then read activeCues.

Frequently Asked Questions

It is an event-handler attribute on a track element. When the text track’s currently displaying cues change during media playback, the cuechange event fires and the JavaScript in oncuechange runs.
Primarily the HTML track element (child of audio or video). The same cuechange event also fires on the underlying TextTrack object.
activeCues is a list on TextTrack of the cues that are active at the current media time. In a cuechange handler, read event.target.track.activeCues (on a track element) and use cue.text for the caption line.
The track mode must be showing or hidden — not disabled. Add the default attribute on track, or set textTrack.mode = "hidden" or "showing" in script after the track loads.
ontimeupdate fires often as currentTime advances. oncuechange fires only when the set of active cues changes — better for caption UI updates.
No. The cuechange event is standard and Baseline widely available. Support on HTMLTrackElement has been widely available since about July 2019.

Next: oncut

Run JavaScript when the user cuts selected content to the clipboard.

oncut tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful