Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 5.4 — Captions and Subtitles
HTML · Example 3 of 3

Switching tracks from JavaScript

This is what the player's CC menu does for you. Click the buttons and watch the console.

The <track> element attaches timed text to a video: captions for people who can't hear it, subtitles for people who don't speak its language, written in a simple text format called WebVTT.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write a WebVTT file with two cues: "Hi, I'm Asha." spoken by Asha from 0 to 2 seconds, and the sound "[phone rings]" from 2.5 to 4 seconds.

02

Attach English captions (on by default) and French subtitles to a video talk.mp4.

03

Write JavaScript that logs "Cue changed" plus the active cue's text each time the captions change on <video id="v">.

Explain it without notes

01

What's the difference between captions and subtitles, and what kind would you use for each?

02

Describe the structure of a WebVTT file and one common mistake when converting from SRT.

03

What do the three text track modes mean, and when is a track's file downloaded?

04

Why are captions required for accessibility, and who else benefits?

Switching tracks from JavaScript

Preview

Press Run to see the page.

Console

Nothing logged yet.

Expected console output

en:disabled hi:showing es:disabled