HTML onratechange Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

ratechange

Speed changed.

03

audio / video

Media only.

04

.playbackRate

Current speed.

05

Custom UI

Speed buttons.

06

addEventListener

Preferred way.

Purpose of onratechange Attribute

The 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×.

💡
Programmatic changes count too

Setting video.playbackRate = 1.5 in JavaScript also fires ratechange — one handler covers both user and code changes.

📝 Syntax

Set onratechange on audio or video, or assign media.onratechange in JavaScript:

onratechange.html
<audio controls onratechange="handleRateChange()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  media.onratechange = handleRateChange;
  media.addEventListener("ratechange", handleRateChange);
</script>

Syntax Rules

  • Value is JavaScript executed when the ratechange event fires.
  • Valid on <audio> and <video> elements.
  • Read element.playbackRate inside the handler for the new speed.
  • Fires when the user changes speed in native controls or when you set playbackRate in code.
  • Also assignable as element.onratechange in script.
  • Modern alternative: media.addEventListener("ratechange", handler).
  • Not for non-media elements like input or div.

💎 Values

The onratechange attribute accepts a string of JavaScript code:

  • onratechange="handleRateChange()" — Call a named function.
  • onratechange="status.textContent = media.playbackRate + 'x'" — Inline update.
  • JavaScript: audio.onratechange = () => { … } — property assignment.
onratechange-handler.html
function handleRateChange() {
  var rate = media.playbackRate;
  status.textContent = "Playback speed: " + rate + "×";
  status.dataset.rate = String(rate);
}

media.addEventListener("ratechange", handleRateChange);

⚡ Quick Reference

Property / eventWhen it firesNotes
ratechangePlayback speed changesonratechange
media.playbackRateRead anytime1 = normal speed
media.defaultPlaybackRateDefault when resetUsually 1
play / pausePlayback stateonplay / onpause
Common speeds0.5, 0.75, 1, 1.25, 1.5, 2Browser-dependent menu

Applicable Elements

ElementSupported?Notes
<audio>YesPrimary use case
<video>YesSame ratechange event
<input>, <div>NoNot media elements
<iframe> (embed)NoCross-origin limits
Custom speed buttonsRelatedSet playbackRate, listen for ratechange

onratechange vs setting playbackRate vs onplay

APIWhat it doesTypical use
onratechangeReact when speed changesUpdate speed label / buttons
media.playbackRate = 1.5Set speed (fires ratechange)Custom speed controls
onplayPlayback starts / resumesPlay button state
onvolumechangeVolume or mute changesVolume slider sync

Examples Gallery

Inline audio handler, dynamic assignment, and custom speed buttons with addEventListener.

👀 Live Preview

Change playback speed from the audio menu — the label updates via ratechange:

Playback speed: 1×

Example — onratechange on audio

Display the new speed when the user changes playback rate:

audio-onratechange.html
<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>
Try It Yourself

How It Works

When playback speed changes, the browser fires ratechange on the media element. The inline attribute runs handleRateChange(), which reads playbackRate.

Dynamic Values with JavaScript

Assign the handler on the media element at runtime:

dynamic-onratechange.html
<script>
  media.onratechange = function () {
    log.textContent = "New playback rate: " + this.playbackRate + "×";
  };
</script>
Try It Yourself

How It Works

Assigning media.onratechange is equivalent to the inline attribute. Use this.playbackRate inside the function when assigned as a property handler.

Example — Custom speed buttons

Set playbackRate from buttons; ratechange keeps the UI synced:

ratechange-buttons.html
btn.addEventListener("click", function () {
  media.playbackRate = parseFloat(btn.dataset.rate);
});

media.addEventListener("ratechange", function () {
  status.textContent = "Playback speed: " + media.playbackRate + "×";
  highlightActiveButton(media.playbackRate);
});
Try It Yourself

How It Works

Setting playbackRate programmatically fires ratechange, so a single listener handles both custom buttons and native control changes.

♿ Accessibility

  • Label speed controls — Use clear button text like “1.5× speed” instead of icons alone.
  • Announce changes — Put the current speed in an element with aria-live="polite".
  • Do not hide native controls — Unless you rebuild them fully accessibly, keep browser media controls available.
  • Keyboard support — Custom speed buttons must be focusable and activatable with Enter/Space.
  • Pitch-corrected audio — Browsers preserve pitch at higher speeds; mention this if users rely on clarity.

🧠 How onratechange Works

1

Speed changes

User or code.

playbackRate
2

ratechange fires

onratechange runs.

Event
3

Read .playbackRate

Update UI.

Sync
=

Speed label

In sync.

Browser Support

The ratechange event and onratechange handler are supported in all modern browsers that support HTML5 media playback rate control.

Media Events · Fully supported

Universal onratechange support

All major browsers fire ratechange when playback speed changes.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
onratechange attribute 99% supported

Bottom line: Reliable for playback-speed UI in all modern browsers.

💡 Best Practices

✅ Do

  • Use onratechange on audio / video
  • Read playbackRate inside the handler
  • Sync custom speed buttons via one ratechange listener
  • Use addEventListener("ratechange", …) in production
  • Offer sensible presets (0.75×, 1×, 1.25×, 1.5×, 2×)

❌ Don’t

  • Rely on console.log for user feedback
  • Put onratechange on non-media elements
  • Assume every browser exposes the same speed menu options
  • Set extreme rates without testing audio clarity
  • Duplicate UI updates in both click handlers and ratechange

Conclusion

The 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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onratechange

Bookmark these before adding speed controls.

5
Core concepts
02

.playbackRate

Read speed.

API
🔄 03

Code + UI

Both fire.

Trigger
🆕 04

Speed buttons

Custom UX.

Pattern
05

Not onplay

Speed only.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the ratechange event fires — when the playback speed of an audio or video element changes.
Most browsers expose a speed option in the native media controls menu (often a gear or “1×” label). You can also set media.playbackRate from custom buttons.
Yes. Assigning 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.
Yes in all modern browsers that support HTML5 media playback rate control.

Build playback-speed controls

Practice the onratechange attribute with inline handlers and custom speed buttons in the Try It editor.

Try onratechange demo →

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.

5 people found this page helpful