HTML onpause Attribute

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

Introduction

The onpause attribute is an inline event handler that runs JavaScript when the pause event fires on an audio or video element. It runs when playback is paused — by the user clicking pause, pressing space, or when your code calls element.pause(). Use it to update a custom UI, save playback position, pause related animations, or log analytics. Pair with onplay when playback resumes. Check media.paused for the current state. Prefer addEventListener("pause", …) in production.

What You’ll Learn

01

Event handler

Inline JS.

02

pause

Playback stops.

03

audio / video

Media only.

04

.paused

Boolean state.

05

+ onplay

Resume UX.

06

addEventListener

Preferred way.

Purpose of onpause Attribute

The primary purpose of onpause is to react when media playback stops temporarily — so your page can sync custom controls, hide a “now playing” indicator, pause background effects, or autosave the current timestamp for resume later.

The pause event fires on the media element itself. It is not the same as ended (media finished) or waiting (buffering). Use the right event for each scenario.

💡
Programmatic pause counts too

Calling video.pause() in JavaScript also fires the pause event — your handler runs either way.

📝 Syntax

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

onpause.html
<audio controls onpause="handlePause()" onplay="handlePlay()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  media.onpause = handlePause;
</script>

Syntax Rules

  • Value is JavaScript executed when the pause event fires.
  • Valid on <audio> and <video> elements.
  • Also assignable as element.onpause in script.
  • Pair with onplay to update UI when playback resumes.
  • Read element.currentTime inside the handler for position.
  • Modern alternative: media.addEventListener("pause", handler).
  • Not for non-media elements like input or div.

💎 Values

The onpause attribute accepts a string of JavaScript code:

  • onpause="handlePause()" — Call a named function.
  • onpause="status.textContent = 'Paused'" — Inline statement.
  • JavaScript: audio.onpause = () => { … } — property assignment.
onpause-handler.html
function handlePause() {
  status.textContent = "Paused at " + formatTime(audio.currentTime);
  status.className = "status-paused";
}

audio.addEventListener("pause", handlePause);
audio.addEventListener("play", handlePlay);

⚡ Quick Reference

Property / eventWhen it firesNotes
pausePlayback pausedonpause
playPlayback starts / resumesonplay
endedMedia reached the endonended
media.pausedAny time (read property)true / false
media.currentTimeSeconds into mediaUseful on pause

Applicable Elements

ElementSupported?Notes
<audio>YesPrimary use case
<video>YesSame pause event
<input>, <div>NoNot media elements
<iframe> (embed)NoCross-origin limits
Custom player UIRelatedListen on underlying media

onpause vs onplay vs onended

AttributeEventTypical use
onpausepauseUpdate UI, save position
onplayplayShow playing state
onendedendedNext track, replay prompt
onwaitingwaitingBuffering spinner

Examples Gallery

Inline audio handler, dynamic assignment, and pause position with addEventListener.

👀 Live Preview

Play and pause the audio — status updates without an alert:

Ready — press play

Example — onpause on audio

Show feedback when the user pauses:

audio-onpause.html
<audio controls onpause="handlePause()" onplay="handlePlay()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
  <p id="status">Ready</p>

<script>
  function handlePause() {
    document.getElementById("status").textContent = "Paused";
  }
  function handlePlay() {
    document.getElementById("status").textContent = "Playing";
  }
</script>
Try It Yourself

How It Works

The browser fires pause on the audio element. The inline attribute runs your handler at that moment.

Dynamic Values with JavaScript

Assign element.onpause after the page loads:

dynamic-onpause.html
<audio controls id="dynamicAudio">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>

<script>
  document.getElementById("dynamicAudio").onpause =
    function () {
      document.getElementById("log").textContent =
        "Dynamic pause handler triggered!";
    };
</script>
Try It Yourself

How It Works

Property assignment on the audio element is equivalent to an inline onpause attribute.

Example — Show pause position

Display where playback stopped using currentTime:

onpause-time.html
audio.addEventListener("pause", function () {
  const secs = Math.floor(audio.currentTime);
  status.textContent = "Paused at " + secs + "s";
});
Try It Yourself

How It Works

currentTime is in seconds. Read it inside the pause handler to save or display the stop position.

♿ Accessibility

  • Keep native controls — Use controls or provide accessible custom buttons.
  • Announce state changes — Use aria-live="polite" on custom status regions.
  • Do not autoplay with sound — Respect user preferences and browser policies.
  • Keyboard support — Space toggles play/pause on focused media elements.
  • Provide transcripts — For audio/video learning content when possible.

🧠 How onpause Works

1

Media playing

Audio / video on.

Playing
2

User pauses

Click or .pause().

Action
3

pause fires

onpause runs.

Event
=

Update UI

Status, save.

Browser Support

The pause event and onpause handler are supported wherever HTML5 audio and video are supported — all modern browsers.

Media Events · Fully supported

Universal onpause support

All major browsers fire pause on audio and video elements.

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+ (media)
Full support
Opera Fully supported
Full support
onpause attribute 99% supported

Bottom line: Reliable for media pause UX in all modern browsers.

💡 Best Practices

✅ Do

  • Pair onpause with onplay
  • Save currentTime on pause for resume
  • Use addEventListener("pause", …) in production
  • Use type="audio/mpeg" for MP3 sources
  • Handle programmatic .pause() the same as user pause

❌ Don’t

  • Use alert() when media pauses
  • Confuse pause with ended
  • Put onpause on non-media elements
  • Block native controls without accessible replacements
  • Assume pause means the user left the page

Conclusion

The onpause attribute runs JavaScript when audio or video playback is paused — essential for custom players, status UI, and saving playback position.

Pair it with onplay, read currentTime on pause, and prefer addEventListener in production code.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about onpause

Bookmark these before building custom media players.

5
Core concepts
🔄 02

+ onplay

Pair both.

Pattern
🔎 03

.paused

State check.

API
04

currentTime

Save spot.

UX
05

Not ended

Mid-stream.

Gotcha

❓ Frequently Asked Questions

It runs JavaScript when the pause event fires — when audio or video playback is paused.
<audio> and <video> — HTML5 media elements only.
No — use onended when media finishes. onpause is for mid-playback pauses.
Yes. Any pause — user action or script — fires the pause event.
media.addEventListener("pause", handler) is preferred for production code.
Yes in all browsers with HTML5 audio and video support.

Build responsive media players

Practice the onpause attribute with audio pause examples in the Try It editor.

Try onpause 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