CKTIS / HTML
onended — handling the end of playback
The onended attribute contains JavaScript executed when an ended event is received. On audio and video elements, it responds to reaching the end of the media, rather than simply pausing playback.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<audio onended="JavaScript statements"></audio>
<video onended="JavaScript statements"></video>onended is an event handler content attribute for ended. Its value is a JavaScript function body, not a file URL or just a function name. It can use event to inspect the event.
For audio and video, the browser fires ended when forward playback reaches the end of the media. Calling pause() alone does not mean playback has ended. With loop set, reaching the end during forward playback returns playback to the beginning instead of firing this event.
Distinguish the HTML attribute onended, the JavaScript property media.onended holding a handler function, and the boolean state property media.ended. An alternative to the attribute is media.addEventListener('ended', handler); the event name has no on prefix.
The example deliberately dispatches a synthetic event, so it needs no media file. It checks attribute wiring, not actual playback completion. A page's security policy may block inline event handler code.
Examples
<!doctype html>
<meta charset="utf-8">
<title>onended test</title>
<audio id="media" onended="document.getElementById('result').textContent = event.type"></audio>
<output id="result">waiting</output>
<script>
const media = document.getElementById('media');
media.dispatchEvent(new Event('ended'));
</script>Result
ended
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.