Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

loop — repeating media playback

The boolean loop attribute on audio and video enables repetition: media returns to the beginning when it reaches the end. It does not start playback by itself.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

<audio loop></audio>
<video loop></video>

loop applies to audio and video. During forward playback, it makes the media seek back to the beginning of the resource upon reaching the end. It does not replace autoplay or a command to start playback.

This is a boolean attribute: presence enables it, and absence disables it. Valid forms are loop, loop="", and loop="loop". Writing loop="false" is nonconforming and still enables looping because the attribute is present.

In JavaScript, the media.loop property reflects the attribute. Assigning true adds it, and false removes it. During looping forward playback, do not expect an ended event at the end of each repetition.

The example checks this property without a media file. The expected text is true, false, false, on separate lines; this is not a test of actual playback repetition.

Examples

<!doctype html>
<meta charset="utf-8">
<title>loop</title>
<audio id="media" loop></audio>
<pre id="output"></pre>
<script>
  const media = document.getElementById('media');
  const results = [String(media.loop)];
  media.loop = false;
  results.push(String(media.loop));
  results.push(String(media.hasAttribute('loop')));
  document.getElementById('output').textContent = results.join('\n');
</script>

Result

true
false
false

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources