CKTIS / HTML
onvolumechange — handle volume or mute changes
The onvolumechange attribute contains JavaScript that handles a media element's volumechange event. It responds to changes in volume or muted, not just to movement of a volume slider.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<audio onvolumechange="handler(event)"></audio>
<video onvolumechange="handler(event)"></video>onvolumechange is a global HTML event-handler attribute. Its value is JavaScript code executed for the volumechange event. Although the attribute can appear on any HTML element, the browser naturally dispatches this event only to media elements: audio and video.
Changing the DOM property volume or muted queues the event; handling occurs after the corresponding setter finishes, not synchronously during assignment. Assigning the same value does not queue another event. volume specifies a level from 0 to 1, whereas muted controls muting independently of that value.
Do not confuse the HTML attribute containing code with the JavaScript property onvolumechange, which takes a function. An alternative is addEventListener("volumechange", handler) — the event name has no on prefix.
In the example, the button toggles muted, and the onvolumechange attribute displays event.type. No media file or playback is needed. The example requires JavaScript and event handlers written in attributes to be enabled.
Examples
<!doctype html>
<meta charset="utf-8">
<title>onvolumechange</title>
<audio id="media" onvolumechange="document.getElementById('result').textContent = event.type"></audio>
<button type="button" onclick="const media = document.getElementById('media'); media.muted = !media.muted;">Toggle mute</button>
<output id="result" aria-live="polite"></output>Result
volumechange
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.