onvolumechange event

Definition and usage

The onvolumechange event occurs each time the volume of video/audio is changed.

This event is triggered by the following situations:

  • Increase or decrease the volume
  • Mute or unmute the media player

Tip:Please use the audio/video object's volume property To set or return the volume of audio/video.

Example

Example 1

Execute JavaScript when the video volume changes:

<video onvolumechange="myFunction()">

Try it yourself

Example 2

Execute JavaScript when the audio volume changes:

<audio onvolumechange="myFunction()">

Try it yourself

Example 3

Use the volume property to set the volume:

document.getElementById("myVideo").volume = 0.2;

Try it yourself

Syntax

In HTML:

<element onvolumechange="myScript">

Try it yourself

In JavaScript:

object.onvolumechange = function(){myScript};

Try it yourself

In JavaScript, use the addEventListener() method:

object.addEventListener("volumechange", myScript);

Try it yourself

Note:Internet Explorer 8 or earlier versions do not support addEventListener() method.

Technical details

Bubbling: Not supported
Cancelable: Not supported
Event type: Event
Supported HTML tags: <audio> and <video>
DOM version: Nivå 3-händelser

Webbläsarstöd

Numrerna i tabellen anger den första webbläsaren som fullständigt stöder händelsen.

Händelser Chrome IE Firefox Safari Opera
onvolumechange Stöd 9.0 Stöd Stöd Stöd