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.
Syntax
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).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Put onended on the media element — not on <source>.
User plays the clip to the end (short sample files help).
ended fires when the end is reached.
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>
MDN rule: with loop and non-negative playbackRate, ended does not fire.
Use timeupdate (or a custom counter) if you need “each lap” logic while looping.
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.
Compatibility
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 ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
onendedWidely supported
Bottom line: Safe to use for finish/replay/playlist UX. Prefer addEventListener('ended') in apps, and remember loop suppresses ended.
Remember
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.