HTML onended Attribute

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

Definition and Usage

The onended attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the ended event fires — playback or streaming stopped because the end of the media was reached, or because no further data is available.

Use it for “thanks for watching,” replay buttons, or playlists. Related: onpause, ontimeupdate, and loop (when loop is on with a non-negative playbackRate, MDN notes ended does not fire).

Baseline · Widely available.

Not deprecated and not experimental. Per MDN, ended is not cancelable and does not bubble. Stable since about July 2015.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer media.addEventListener("ended", handler) in production; use inline onended for quick demos and learning.

HTML Syntax

syntax.html
<video|audio onended=" script">

script is JavaScript that runs when playback finishes. Equivalent JS: media.onended = handler or media.addEventListener("ended", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when playback reaches the endTry it
onended="fn()"Call a named function from the attributeTry it
addEventListener("ended")Preferred modern listener — e.g. seek to 0 and replayTry it
With loopWhen looping with non-negative rate, ended does not fireTry it

Live Preview

Play the clip to the end. When playback finishes, ended updates the status. Toggle loop to see that ended does not fire while looping.

Media

Sample media from /video/count.mp4 or /audio/count.mp3. Play through to the end.

Player

Waiting for ended…

HTML Examples

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

Example 1 — Message When Playback Ends

Update a status line when the clip finishes.

HTML
<audio id="clip" controls onended="onFinish()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Playing… (or press play)</p>

<script>
  function onFinish() {
    document.getElementById("status").textContent =
      "ended — playback finished.";
  }
</script>
Try It Yourself

How It Works

  1. Put onended on the media element — not on <source>.
  2. User plays the clip to the end (short sample files help).
  3. ended fires when the end is reached.
  4. Update UI — message, next button, or analytics hook.

Example 2 — Replay From the Start

On ended, seek to 0 and call play() again.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
</video>
<p id="log">Play to the end to auto-replay.</p>

<script>
  var clip = document.getElementById("clip");
  var log = document.getElementById("log");
  var times = 0;

  clip.addEventListener("ended", function () {
    times += 1;
    log.textContent = "ended #" + times + " — replaying…";
    clip.currentTime = 0;
    clip.play();
  });
</script>
Try It Yourself

How It Works

  1. Listen for ended with addEventListener.
  2. Set currentTime = 0 to return to the start.
  3. Call play() — may need a user gesture on some browsers for the first play.
  4. Prefer loop for endless replay unless you need custom logic between loops.

Example 3 — loop Prevents ended

With loop, the media restarts — ended does not fire (non-negative rate).

HTML
<audio id="clip" controls loop>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">loop is on — ended should not fire.</p>

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

  clip.addEventListener("ended", function () {
    document.getElementById("status").textContent =
      "ended fired (unexpected with loop).";
  });

  clip.addEventListener("timeupdate", function () {
    if (clip.currentTime < 0.2) {
      document.getElementById("status").textContent =
        "Looping — currentTime near start again.";
    }
  });
</script>
Try It Yourself

How It Works

  1. loop tells the browser to restart at the end.
  2. MDN rule: with loop and non-negative playbackRate, ended does not fire.
  3. Use timeupdate (or a custom counter) if you need “each lap” logic while looping.
  4. Turn loop off when you want a true finished event for playlists.

Edge Cases & Pitfalls

Most “ended never fired” bugs are loop still enabled.

loop

loop skips ended

If loop is true and playbackRate is non-negative, the media restarts and ended does not fire.

pause

Not the same as pause

User pause, script pause, and buffering pauses use onpause. Finished playback uses ended.

api

Not cancelable

You cannot stop ended with preventDefault(). React after it fires.

target

Listen on audio/video, not source

The event targets the media element. Nested <source> tags do not receive onended.

autoplay

Replay may need a gesture

Calling play() again from ended often works after the user already started playback, but first-play autoplay policies still apply on some browsers.

Browser Support

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

✓ Baseline · Widely available

onended

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support ended on audio and video.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onended Widely supported

Bottom line: Safe to use for finish/replay/playlist UX. Prefer addEventListener('ended') in apps, and remember loop suppresses ended.

Key Takeaways

  • Role: playback stopped because the media ended (or no more data).
  • Not cancelable and does not bubble.
  • loop: with looping + non-negative rate, ended does not fire.
  • Uses: status messages, replay, playlists — not ordinary pause handling.

One line: onended means the media finished — turn loop off if you need that event.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When playback stops because the end of the media was reached (or no further data is available), the ended event fires and the JavaScript in onended runs.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
No. Per MDN, ended does not fire if the loop property is true and playbackRate is non-negative — the media restarts instead of finishing.
No. Per MDN, ended is not cancelable and does not bubble. You cannot stop it with preventDefault().
pause fires whenever playback is paused (user click, script, buffering). ended fires specifically when playback stops because the media finished (or no more data).
No. The ended event and onended handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onerror

Handle errors when media (or other resources) fail to load.

onerror tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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