👀 Live Preview
Change playback speed from the audio menu — the label updates via ratechange:
Playback speed: 1×

The onratechange attribute is an inline event handler for the ratechange event on audio or video elements. It runs when the playback speed changes — for example when the user picks 1.5× from the browser’s speed menu or when your code sets media.playbackRate. Read media.playbackRate inside the handler (1 = normal, 0.5 = half speed, 2 = double). Use it to update custom speed buttons, show a speed label, or log analytics. Prefer addEventListener("ratechange", …) in production.
Inline JS.
Speed changed.
Media only.
Current speed.
Speed buttons.
Preferred way.
onratechange AttributeThe primary purpose of onratechange is to react when playback speed changes — so your page can keep custom controls, labels, and analytics in sync with the actual rate, whether the user changed it from native controls or your JavaScript did.
The ratechange event fires on the media element whenever playbackRate changes. Default speed is 1 (100%). Podcast apps, language-learning sites, and video players often expose speed presets like 0.75× or 2×.
Setting video.playbackRate = 1.5 in JavaScript also fires ratechange — one handler covers both user and code changes.
Set onratechange on audio or video, or assign media.onratechange in JavaScript:
<audio controls onratechange="handleRateChange()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onratechange = handleRateChange;
media.addEventListener("ratechange", handleRateChange);
</script>ratechange event fires.<audio> and <video> elements.element.playbackRate inside the handler for the new speed.playbackRate in code.element.onratechange in script.media.addEventListener("ratechange", handler).input or div.The onratechange attribute accepts a string of JavaScript code:
onratechange="handleRateChange()" — Call a named function.onratechange="status.textContent = media.playbackRate + 'x'" — Inline update.audio.onratechange = () => { … } — property assignment.function handleRateChange() {
var rate = media.playbackRate;
status.textContent = "Playback speed: " + rate + "×";
status.dataset.rate = String(rate);
}
media.addEventListener("ratechange", handleRateChange);| Property / event | When it fires | Notes |
|---|---|---|
ratechange | Playback speed changes | onratechange |
media.playbackRate | Read anytime | 1 = normal speed |
media.defaultPlaybackRate | Default when reset | Usually 1 |
play / pause | Playback state | onplay / onpause |
| Common speeds | 0.5, 0.75, 1, 1.25, 1.5, 2 | Browser-dependent menu |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same ratechange event |
<input>, <div> | No | Not media elements |
<iframe> (embed) | No | Cross-origin limits |
| Custom speed buttons | Related | Set playbackRate, listen for ratechange |
onratechange vs setting playbackRate vs onplay| API | What it does | Typical use |
|---|---|---|
onratechange | React when speed changes | Update speed label / buttons |
media.playbackRate = 1.5 | Set speed (fires ratechange) | Custom speed controls |
onplay | Playback starts / resumes | Play button state |
onvolumechange | Volume or mute changes | Volume slider sync |
Inline audio handler, dynamic assignment, and custom speed buttons with addEventListener.
Change playback speed from the audio menu — the label updates via ratechange:
Playback speed: 1×
Display the new speed when the user changes playback rate:
<audio controls id="myMedia" onratechange="handleRateChange()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Playback speed: 1×</p>
<script>
function handleRateChange() {
var rate = document.getElementById("myMedia").playbackRate;
document.getElementById("status").textContent =
"Playback speed: " + rate + "×";
}
</script>When playback speed changes, the browser fires ratechange on the media element. The inline attribute runs handleRateChange(), which reads playbackRate.
Assign the handler on the media element at runtime:
<script>
media.onratechange = function () {
log.textContent = "New playback rate: " + this.playbackRate + "×";
};
</script>Assigning media.onratechange is equivalent to the inline attribute. Use this.playbackRate inside the function when assigned as a property handler.
Set playbackRate from buttons; ratechange keeps the UI synced:
btn.addEventListener("click", function () {
media.playbackRate = parseFloat(btn.dataset.rate);
});
media.addEventListener("ratechange", function () {
status.textContent = "Playback speed: " + media.playbackRate + "×";
highlightActiveButton(media.playbackRate);
});Setting playbackRate programmatically fires ratechange, so a single listener handles both custom buttons and native control changes.
aria-live="polite".User or code.
onratechange runs.
Update UI.
In sync.
The ratechange event and onratechange handler are supported in all modern browsers that support HTML5 media playback rate control.
onratechange supportAll major browsers fire ratechange when playback speed changes.
Bottom line: Reliable for playback-speed UI in all modern browsers.
onratechange on audio / videoplaybackRate inside the handlerratechange listeneraddEventListener("ratechange", …) in productionconsole.log for user feedbackonratechange on non-media elementsThe onratechange attribute runs JavaScript when playback speed changes on audio or video — essential for keeping custom speed controls and labels in sync.
Read playbackRate in your handler, use one ratechange listener for both native and custom changes, and prefer addEventListener in production code.
onratechangeBookmark these before adding speed controls.
audio / video.
ScopeRead speed.
APIBoth fire.
TriggerCustom UX.
PatternSpeed only.
Gotcharatechange event fires — when the playback speed of an audio or video element changes.media.playbackRate from custom buttons.media.playbackRate programmatically triggers the ratechange event, so your handler runs for both user and code changes.1 — normal speed. 0.5 is half speed; 2 is double speed.media.addEventListener("ratechange", handler) is preferred in production — cleaner separation and multiple listeners if needed.Practice the onratechange attribute with inline handlers and custom speed buttons in the Try It editor.
5 people found this page helpful