HTML onvolumechange Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onvolumechange attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the volumechange event fires — either the volume property or the muted property changed.

Use it to sync a custom volume UI, show a mute icon, or log level changes. Related: muted, controls, onplay, and onpause.

Baseline · Widely available.

Not deprecated and not experimental. Per MDN, volumechange is not cancelable and does not bubble. Stable since about July 2015.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer media.addEventListener("volumechange", handler) in production; use inline onvolumechange for quick demos and learning.

HTML Syntax

syntax.html
<video|audio onvolumechange=" script">

script is JavaScript that runs when volume or mute changes. Equivalent JS: media.onvolumechange = handler or media.addEventListener("volumechange", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when volume or mute changesTry it
onvolumechange="fn()"Named handler; read volume and mutedTry it
Custom sliderSet media.volume from a range inputTry it
Mute toggleFlip media.muted — still fires volumechangeTry it

Live Preview

Use the player controls (or the buttons) to change volume or mute. Status updates on volumechange.

Media

Sample from /audio/count.mp3 or /video/count.mp4. Play, then adjust volume.

Player

Waiting for volumechange…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Display Volume and Mute

Update status whenever the player’s volume or mute changes.

HTML
<audio id="clip" controls onvolumechange="showVol()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Change volume or mute in the controls.</p>

<script>
  function showVol() {
    var clip = document.getElementById("clip");
    document.getElementById("status").textContent =
      "volume=" + clip.volume.toFixed(2) +
      " · muted=" + clip.muted;
  }
</script>
Try It Yourself

How It Works

  1. Put onvolumechange on the media element — not on <source>.
  2. User (or script) changes volume or mute.
  3. volumechange fires after the property updates.
  4. Read volume (0–1) and muted for your UI.

Example 2 — Custom Volume Slider

Drive media.volume from a range input; keep the label in sync via volumechange.

HTML
<audio id="clip" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<label for="vol">Volume: <span id="label">100%</span></label>
<input id="vol" type="range" min="0" max="100" value="100">

<script>
  var clip = document.getElementById("clip");
  var vol = document.getElementById("vol");
  var label = document.getElementById("label");

  vol.addEventListener("input", function () {
    clip.volume = vol.value / 100;
  });

  clip.addEventListener("volumechange", function () {
    label.textContent = Math.round(clip.volume * 100) + "%";
    vol.value = String(Math.round(clip.volume * 100));
  });
</script>
Try It Yourself

How It Works

  1. On slider input, set clip.volume (0.0–1.0).
  2. That assignment fires volumechange.
  3. In the handler, refresh the percent label (and optionally the slider).
  4. Native controls can still change volume — your handler stays in sync.

Example 3 — Mute Button

Toggle muted; the same event updates the button label.

HTML
<audio id="clip" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<button type="button" id="muteBtn">Mute</button>
<p id="note">muted=false</p>

<script>
  var clip = document.getElementById("clip");
  var muteBtn = document.getElementById("muteBtn");
  var note = document.getElementById("note");

  muteBtn.addEventListener("click", function () {
    clip.muted = !clip.muted;
  });

  clip.addEventListener("volumechange", function () {
    note.textContent = "muted=" + clip.muted;
    muteBtn.textContent = clip.muted ? "Unmute" : "Mute";
  });
</script>
Try It Yourself

How It Works

  1. Flip clip.muted in the button click handler.
  2. volumechange fires for mute changes too (per MDN).
  3. Update UI from clip.muted in one place.
  4. Note: mute does not reset volume — the previous level returns when unmuted.

Edge Cases & Pitfalls

Most confusion is treating mute as “volume = 0” instead of a separate muted flag.

mute

Mute ≠ volume 0

muted silences playback while keeping the last volume value. Always check both properties in your handler.

range

volume is 0.0–1.0

Not 0–100. Divide percent sliders by 100 when assigning media.volume.

api

Not cancelable

You cannot stop volumechange with preventDefault(). React after it fires.

target

Listen on audio/video

The event targets the media element. Nested <source> tags do not receive onvolumechange.

os

System volume is separate

OS / device master volume usually does not fire this event — only the element’s volume / muted.

Browser Support

onvolumechange is an HTML event-handler attribute for media elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onvolumechange

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support volumechange on audio and video.

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onvolumechange Widely supported

Bottom line: Safe to use for custom volume/mute UI. Prefer addEventListener('volumechange') in apps, and always read both volume and muted.

Key Takeaways

  • Role: media volume or muted changed.
  • Not cancelable and does not bubble.
  • volume is 0.0–1.0; mute is a separate boolean.
  • One handler covers both slider and mute changes.

One line: onvolumechange means the media got louder, quieter, or muted — read volume and muted.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When the volume property or the muted property changes, the volumechange event fires and the JavaScript in onvolumechange runs.
HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
Yes. Per MDN, volumechange fires when either volume or muted changes — so one handler covers both the volume slider and mute.
A number from 0.0 (silent) to 1.0 (loudest). Setting it in script or via the browser controls fires volumechange.
No. Per MDN, volumechange is not cancelable and does not bubble.
No. The volumechange event and onvolumechange handler are standard HTML media features and Baseline widely available since about July 2015. Inline HTML handlers are discouraged in production in favor of addEventListener.

Next: onwaiting

Handle when media playback stops temporarily waiting for data.

onwaiting tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful