👀 Live Preview
Play the audio to the end — status updates when ended fires:
Press play and listen to the end…

The onended attribute is an inline event handler that runs JavaScript when the ended event fires. That happens on <audio> and <video> when playback reaches the end of the media naturally. Use it to show a replay button, play the next track in a playlist, display a “Thanks for watching” message, or log analytics. It does not fire when the user pauses mid-playback (that is onpause) or when media is reset (that is onemptied). With the loop attribute enabled, ended typically does not fire because playback restarts automatically.
Inline JS.
Playback done.
Media elements.
Show replay btn.
Preferred way.
Property API.
onended AttributeThe primary purpose of onended is to react when media playback completes. You might reveal a replay button, advance to the next song, show end-screen credits, or trigger completion tracking. It is a one-time event at the natural end of the timeline — unlike ontimeupdate, which fires continuously during playback.
Pair onended with playlist logic in JavaScript: when one clip finishes, load and play the next. For single clips, update UI to invite the user to replay or navigate elsewhere.
Production code should use audio.addEventListener("ended", handler). Inline onended on <audio> or <video> works for learning but separates concerns less cleanly.
Set onended on audio or video, or assign element.onended:
<script>
function handleMediaEnd() {
console.log("Playback finished.");
}
</script>
<audio controls onended="handleMediaEnd()">
<source src="song.mp3" type="audio/mpeg">
</audio>ended event fires.<audio> and <video> media elements.media.onended = function() { … }.media.addEventListener("ended", handler).loop is enabled (media restarts).The onended attribute accepts a string of JavaScript code:
onended="handleMediaEnd()" — Call a named function.onended="showReplay()" — Inline statement.video.onended = () => { … } assigns the handler.const video = document.getElementById("clip");
video.addEventListener("ended", () => {
document.getElementById("replay-btn").hidden = false;
});
document.getElementById("replay-btn").addEventListener("click", () => {
video.currentTime = 0;
video.play();
document.getElementById("replay-btn").hidden = true;
});| Scenario | ended fires? | Notes |
|---|---|---|
| Playback reaches end | Yes | Primary use case |
| User pauses mid-clip | No | Use onpause |
loop attribute set | Usually no | Media restarts |
| User seeks to end manually | Varies | May fire if playback continues to end |
| Handler attribute | onended | Inline on media tag |
| Applicable elements | audio, video | Media elements only |
| Target | Supported? | Notes |
|---|---|---|
<audio> | Yes | Inline onended on audio |
<video> | Yes | End screens, replay buttons |
<source> | No | Event fires on parent media element |
<input>, <img> | No | Not media playback events |
onended vs onpause vs ontimeupdate| Handler | When it fires | Typical use |
|---|---|---|
onended | Playback reaches the end | Replay, next track, end screen |
onpause | Playback paused (user or script) | Save position, update UI |
ontimeupdate | currentTime changes during play | Progress bar updates |
Inline onended on audio, addEventListener on video, and a simple replay pattern.
Play the audio to the end — status updates when ended fires:
Press play and listen to the end…
Run a function when playback completes:
<script>
function handleMediaEnd() {
console.log("Media playback has ended!");
}
</script>
<audio controls onended="handleMediaEnd()">
<source src="song.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>The browser fires ended on the audio element when playback completes. The inline onended attribute wires up the handler.
Attach the handler with addEventListener:
<script>
document.getElementById("dynamicAudio").addEventListener("ended", function () {
console.log("Custom ended event triggered!");
});
// Or property form:
document.getElementById("dynamicAudio").onended = function () { /* … */ };
</script>Register the listener once at page load. When playback finishes, your completion logic runs once.
Reveal a replay control when the video ends:
const video = document.getElementById("clip");
const replay = document.getElementById("replay");
video.addEventListener("ended", () => {
replay.hidden = false;
});
replay.addEventListener("click", () => {
video.currentTime = 0;
video.play();
replay.hidden = true;
});onended is the hook for post-playback UX — replay, autoplay next, or end-screen content.
Audio or video starts from currentTime.
timeupdate fires during playback.
ended fires; onended runs.
Replay, next track, or end screen.
The ended event and onended handler are supported in all modern browsers on <audio> and <video> elements — Chrome, Firefox, Safari, and Edge.
onended supportAll major browsers fire the underlying event and honor the onended handler attribute.
Bottom line: Use onended confidently on media elements; prefer addEventListener in production code.
ended firesaddEventListener("ended", …) in production codeloop attributealert() when media finishesended with pause or emptiedended fires when loop is enabled<source> instead of the media elementThe onended attribute runs JavaScript when audio or video playback reaches its natural end — the right hook for replay buttons, playlists, and completion messages.
Prefer addEventListener("ended", …), distinguish ended from pause and timeupdate events, and remember that looping media usually skips ended between repeats.
onendedBookmark these before building your next media player.
Natural end.
EventMedia only.
ScopeCommon pattern.
UXPreferred.
Modernloop skips end.
Compareended event fires — when playback reaches the end of the media.<audio> and <video>. Attach handlers to the media element, not individual <source> tags.pause. ended fires only when playback completes to the end.ended typically does not fire between loops.addEventListener("ended", handler) is preferred for production. Inline onended is fine for tutorials and quick demos.audio and video elements.Practice the onended attribute with replay and playlist examples in the Try It editor.
5 people found this page helpful