👀 Live Preview
Adjust volume or mute on the audio controls — the label updates via volumechange:
Volume: 100%

The onvolumechange attribute is an inline event handler that runs JavaScript when the volumechange event fires on an audio or video element. It runs when the volume level or muted state changes — for example when the user drags the volume slider, clicks the mute button, or when your code sets media.volume or media.muted. Read media.volume (0.0 to 1.0) and media.muted inside the handler. Use it to sync custom volume sliders, show a volume percentage label, or save user preferences. Prefer addEventListener("volumechange", …) in production.
Inline JS.
Volume or mute.
Media only.
0.0 – 1.0.
Volume sliders.
Preferred way.
onvolumechange AttributeThe primary purpose of onvolumechange is to react when volume or mute state changes — so your page can keep custom controls, labels, and saved preferences in sync with the actual media state, whether the user changed it from native controls or your JavaScript did.
The volumechange event fires on the media element whenever volume or muted changes. Default volume is 1 (100%). Video players, podcasts, and music sites often show a custom slider or percentage readout alongside native controls.
Clicking the mute button sets media.muted and also fires volumechange — one handler covers both volume slider and mute changes.
Set onvolumechange on audio or video, or assign media.onvolumechange in JavaScript:
<audio controls onvolumechange="updateVolume()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onvolumechange = updateVolume;
media.addEventListener("volumechange", updateVolume);
</script>volumechange event fires.<audio> and <video> elements.element.volume (0.0–1.0) and element.muted inside the handler.volume / muted in code.element.onvolumechange in script.media.addEventListener("volumechange", handler).input or div.The onvolumechange attribute accepts a string of JavaScript code:
onvolumechange="updateVolume()" — Call a named function.onvolumechange="status.textContent = Math.round(media.volume * 100) + '%'" — Inline update.audio.onvolumechange = () => { … } — property assignment.function updateVolume() {
var pct = Math.round(media.volume * 100);
status.textContent = media.muted
? "Muted"
: "Volume: " + pct + "%";
status.dataset.volume = String(media.volume);
}
media.addEventListener("volumechange", updateVolume);| Property / event | When it fires | Notes |
|---|---|---|
volumechange | Volume or mute changes | onvolumechange |
media.volume | Read anytime | 0.0 = silent, 1.0 = full |
media.muted | Read anytime | true when muted |
media.defaultMuted | Initial mute state | Set via muted attribute |
ratechange | Playback speed changes | onratechange — different event |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same volumechange event |
<input type="range"> | Related | Custom slider — sync with media.volume |
<input>, <div> | No | Not media elements |
<iframe> (embed) | No | Cross-origin limits |
onvolumechange vs setting volume vs onratechange| API | What it does | Typical use |
|---|---|---|
onvolumechange | React when volume or mute changes | Update volume label / slider |
media.volume = 0.5 | Set volume (fires volumechange) | Custom volume controls |
media.muted = true | Mute (fires volumechange) | Mute toggle button |
onratechange | Playback speed changes | Speed label sync |
Inline audio handler, dynamic assignment, and a custom volume slider with addEventListener.
Adjust volume or mute on the audio controls — the label updates via volumechange:
Volume: 100%
Display the new volume when the user adjusts the native controls:
<audio controls id="myMedia" onvolumechange="updateVolume()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Volume: 100%</p>
<script>
function updateVolume() {
var media = document.getElementById("myMedia");
var pct = Math.round(media.volume * 100);
document.getElementById("status").textContent =
media.muted ? "Muted" : "Volume: " + pct + "%";
}
</script>When volume or mute changes, the browser fires volumechange on the media element. The inline attribute runs updateVolume(), which reads volume and muted.
Assign the handler on the media element at runtime:
<script>
media.onvolumechange = function () {
log.textContent = this.muted
? "Audio muted"
: "Volume: " + Math.round(this.volume * 100) + "%";
};
</script>Assigning media.onvolumechange is equivalent to the inline attribute. Use this.volume and this.muted inside the function when assigned as a property handler.
Set volume from a range input; volumechange keeps the UI synced when native controls change too:
slider.addEventListener("input", function () {
media.volume = parseFloat(slider.value);
});
media.addEventListener("volumechange", function () {
slider.value = String(media.volume);
status.textContent = media.muted
? "Muted"
: "Volume: " + Math.round(media.volume * 100) + "%";
});Setting volume programmatically fires volumechange, so a single listener handles both custom slider and native control changes without duplicating update logic.
aria-label on custom sliders like “Volume” or “Playback volume.”aria-live="polite".User or code.
onvolumechange runs.
Update UI.
In sync.
The volumechange event and onvolumechange handler are supported in all modern browsers that support HTML5 media volume control.
onvolumechange supportAll major browsers fire volumechange when volume or mute state changes.
Bottom line: Reliable for volume UI in all modern browsers.
onvolumechange on audio / videovolume and muted inside the handlervolumechange listeneraddEventListener("volumechange", …) in productionmuted is trueconsole.log or alert() for user feedbackonvolumechange on non-media elementsvolume === 0 means muted (check muted too)media.volumeThe onvolumechange attribute runs JavaScript when volume or mute state changes on audio or video — essential for keeping custom volume controls and labels in sync.
Read volume and muted in your handler, use one volumechange listener for both native and custom changes, and prefer addEventListener in production code.
onvolumechangeBookmark these before adding volume controls.
audio / video.
Scope0.0 – 1.0.
APIAlso fires.
TriggerCustom UX.
PatternVolume only.
Gotchavolumechange event fires — when the volume or muted state of an audio or video element changes.0.0 (silent) to 1.0 (full volume). Multiply by 100 for a percentage display. Default is 1.media.muted = true or clicking the mute button fires volumechange. Check media.muted in your handler — volume may still be non-zero while muted.media.volume programmatically triggers the volumechange event, so your handler runs for both user and code changes.media.addEventListener("volumechange", handler) is preferred in production — cleaner separation and multiple listeners if needed.Practice the onvolumechange attribute with inline handlers and custom volume sliders in the Try It editor.
5 people found this page helpful