HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML onended Attribute
Photo Credit to CodeToFun
🙋 Introduction
The onended
attribute is a powerful feature in HTML that allows developers to specify a JavaScript function to be executed when a media element, such as an audio or video player, finishes playing its content.
This attribute provides a way to trigger actions or events based on the completion of media playback.
🎯 Purpose of onended
The primary purpose of the onended
attribute is to enable developers to define custom behavior or execute specific actions when a media element reaches the end of its content.
This can be particularly useful for creating interactive and dynamic web applications that respond to the completion of audio or video playback.
💎 Values
The onended
attribute accepts a JavaScript function as its value.
This function will be executed when the media element completes its playback. Here's a simple example:
<audio controls onended="myFunction()">
<source src="audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
🧠 How it Works
In this example, the myFunction() JavaScript function will be called when the audio playback reaches its end. You can replace "myFunction()" with the name of your custom function.
📄 Example
Let's look at a more comprehensive example of how to use the onended
attribute in conjunction with JavaScript:
<script>
function handleMediaEnd() {
alert("Media playback has ended!");
// Additional custom actions can be added here
}
</script>
<audio controls onended="handleMediaEnd()">
<source src="audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
🧠 How it Works
In this example, the handleMediaEnd() function will be executed when the audio playback concludes. You can customize this function to perform any desired actions.
🔄 Dynamic Values with JavaScript
You can also dynamically set the onended
attribute using JavaScript.
This is beneficial when you want to change the behavior based on user interactions or other conditions. Here's a brief example:
<script>
// Dynamically set onended for an audio element
document.getElementById("dynamicAudio").onended = function() {
console.log("Custom onended event triggered!");
// Additional custom actions can be added here
};
</script>
<audio controls id="dynamicAudio">
<source src="dynamic_audio.mp3" type="audio/mp3">
Your browser does not support the audio element.
</audio>
🧠 How it Works
In this script, the onended
attribute is dynamically set for an audio element with the id "dynamicAudio." This allows you to define a custom function for handling the end of playback dynamically.
🏆 Best Practices
- Utilize the
onended
attribute to enhance user experience by triggering relevant actions or events when media playback concludes. - Ensure that the JavaScript function assigned to onended is well-defined and performs the intended actions.
- Test your implementation across different browsers to ensure compatibility.
🎉 Conclusion
The onended
attribute provides a valuable tool for developers working with media elements in HTML.
By leveraging this attribute, you can create more interactive and responsive web applications that respond to the completion of audio or video playback.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML onended Attribute), please comment here. I will help you immediately.