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 ondurationchange Attribute
Photo Credit to CodeToFun
🙋 Introduction
The ondurationchange
attribute in HTML is an event handler that is triggered when the duration attribute of a media element changes.
This event is commonly associated with audio and video elements, providing developers with a way to respond to changes in the media duration dynamically.
🎯 Purpose of ondurationchange
The primary purpose of the ondurationchange
attribute is to enable developers to execute JavaScript code in response to changes in the duration of a media element.
This can be useful for scenarios where you want to update the user interface, display additional information, or perform other actions based on the length of the media.
💎 Values
The ondurationchange
attribute is an event handler, and as such, it doesn't have specific values like some other attributes.
Instead, it is assigned a JavaScript function that will be executed when the durationchange event occurs.
📄 Example
Let's look at a simple example of how to use the ondurationchange
attribute with a video element:
<video controls ondurationchange="updateDuration()">
<source src="example.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
<script>
function updateDuration() {
var video = document.querySelector('video');
console.log('New duration: ' + video.duration);
// Additional actions can be performed based on the updated duration
}
</script>
🧠 How it Works
In this example, the ondurationchange
attribute is set to the JavaScript function updateDuration().
This function logs the new duration of the video element to the console. You can customize the function to perform actions based on the updated duration.
🔄 Dynamic Values with JavaScript
Similar to other event handlers, you can dynamically assign the ondurationchange
attribute using JavaScript.
This is helpful when you want to set event handlers based on certain conditions or user interactions. Here's an example:
<script>
// Dynamically set ondurationchange for a video element
var videoElement = document.getElementById("dynamicVideo");
videoElement.ondurationchange = function() {
console.log('Dynamic duration change detected!');
// Additional actions can be performed based on the updated duration
};
</script>
🧠 How it Works
In this script, the ondurationchange
attribute is dynamically set for a video element with the id "dynamicVideo."
The associated function logs a message to the console, but you can customize it to suit your specific requirements.
🏆 Best Practices
- Use the
ondurationchange
attribute when you need to respond to changes in the duration of media elements dynamically. - Always consider user experience when implementing event handlers, ensuring that any updates or actions based on duration changes enhance the overall interaction.
- Test your code across different browsers to ensure consistent behavior, as browser support may vary.
🎉 Conclusion
The ondurationchange
attribute provides a valuable mechanism for developers to respond to changes in the duration of media elements in HTML.
By incorporating this attribute into your scripts, you can create more interactive and dynamic user experiences for multimedia content.
👨💻 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 ondurationchange Attribute), please comment here. I will help you immediately.