HTML | DOM onended Event
The HTML DOM onended event occurs when the audio/video is ended. We can add some custom message in this event like “Thank for watching”, “Share”, etc.
Syntax:
In HTML:
<element onended="myScript">
In JavaScript:
object.onended = function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("ended", myScript);
Example: Using HTML
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksfroGeeks</h1> <h2>HTML DOM onended Event</h2> <audio controls onended="gfgFun()"> <source src="beep.mp3" type="audio/mpeg"> </audio> <script> function gfgFun() { alert("Thanks for listning"); } </script> </center> </body> </html> |
Output:


Example: Using JavaScript
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksfroGeeks</h1> <h2>HTML DOM onended Event</h2> <audio id="audioId" controls> <source src="beep.mp3" type="audio/mpeg"> </audio> <p id="try"></p> <script> document.getElementById( "audioId").onended = function() { gfgFun() }; function gfgFun() { document.getElementById( "try").innerHTML = "Thanks for listning"; } </script> </center> </body> </html> |
Output:

Example: Using the addEventListener() method
<!DOCTYPE html> <html> <body> <center> <h1 style="color:green">GeeksfroGeeks</h1> <h2>HTML DOM onended Event</h2> <audio id="audioId" controls> <source src="beep.mp3" type="audio/mpeg"> </audio> <p id="demo"></p> <script> document.getElementById( "audioId").addEventListener("ended", gfgFun); function gfgFun() { document.getElementById( "demo").innerHTML = "Thanks for listning"; } </script> </center> </body> </html> |
Output:

Supported Browsers: The browsers supported by HTML DOM onended Event are listed below:
- Google Chrome
- Internet Explorer 9.0
- Firefox
- Apple Safari
- Opera
Recommended Posts:
- HTML | DOM onloadedmetadata Event
- HTML | DOM oncanplay Event
- HTML | DOM touchend Event
- HTML | DOM onchange Event
- HTML | DOM onstalled Event
- HTML | DOM oncanplaythrough Event
- HTML | DOM onkeypress Event
- HTML | DOM onkeydown Event
- HTML | DOM onblur Event
- HTML | DOM onkeyup Event
- HTML | DOM onloadeddata Event
- HTML | DOM onload Event
- HTML | DOM ontoggle Event
- HTML | DOM ondragend Event
- HTML | DOM onsubmit Event
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



