View video tutorial

HTML onended Attribute

HTML

The HTML onended attribute is an event handler that triggers a script to execute when an audio or video element has finished playing.

Definition and Usage


➔ The primary purpose is to display an end message or load the next media file after the current media reaches its end.

➔ The event does not fire if the loop attribute is set to true.

Syntax
//In HTML
<video controls onended="myFunction()">
  <source src="myvideo.mp4" type="video/mp4">
</video>

//Using Event Handler
x.onended = function () { myFunction() };

//Using addEventListener
x.addEventListener("ended", () => { myFunction(); });

Applies to

This attribute can be used on the following element.

Attribute Element
onended <audio>, <video>