👀 Live Preview
Play and pause the audio — status updates without an alert:
Ready — press play

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.
Inline JS.
Playback stops.
Media only.
Boolean state.
Resume UX.
Preferred way.
onpause AttributeThe 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.
Calling video.pause() in JavaScript also fires the pause event — your handler runs either way.
Set onpause on audio or video, or assign media.onpause in JavaScript:
<audio controls onpause="handlePause()" onplay="handlePlay()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onpause = handlePause;
</script>pause event fires.<audio> and <video> elements.element.onpause in script.onplay to update UI when playback resumes.element.currentTime inside the handler for position.media.addEventListener("pause", handler).input or div.The onpause attribute accepts a string of JavaScript code:
onpause="handlePause()" — Call a named function.onpause="status.textContent = 'Paused'" — Inline statement.audio.onpause = () => { … } — property assignment.function handlePause() {
status.textContent = "Paused at " + formatTime(audio.currentTime);
status.className = "status-paused";
}
audio.addEventListener("pause", handlePause);
audio.addEventListener("play", handlePlay);| Property / event | When it fires | Notes |
|---|---|---|
pause | Playback paused | onpause |
play | Playback starts / resumes | onplay |
ended | Media reached the end | onended |
media.paused | Any time (read property) | true / false |
media.currentTime | Seconds into media | Useful on pause |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same pause event |
<input>, <div> | No | Not media elements |
<iframe> (embed) | No | Cross-origin limits |
| Custom player UI | Related | Listen on underlying media |
onpause vs onplay vs onended| Attribute | Event | Typical use |
|---|---|---|
onpause | pause | Update UI, save position |
onplay | play | Show playing state |
onended | ended | Next track, replay prompt |
onwaiting | waiting | Buffering spinner |
Inline audio handler, dynamic assignment, and pause position with addEventListener.
Play and pause the audio — status updates without an alert:
Ready — press play
Show feedback when the user pauses:
<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>The browser fires pause on the audio element. The inline attribute runs your handler at that moment.
Assign element.onpause after the page loads:
<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>Property assignment on the audio element is equivalent to an inline onpause attribute.
Display where playback stopped using currentTime:
audio.addEventListener("pause", function () {
const secs = Math.floor(audio.currentTime);
status.textContent = "Paused at " + secs + "s";
});currentTime is in seconds. Read it inside the pause handler to save or display the stop position.
controls or provide accessible custom buttons.aria-live="polite" on custom status regions.Audio / video on.
Click or .pause().
onpause runs.
Status, save.
The pause event and onpause handler are supported wherever HTML5 audio and video are supported — all modern browsers.
onpause supportAll major browsers fire pause on audio and video elements.
Bottom line: Reliable for media pause UX in all modern browsers.
onpause with onplaycurrentTime on pause for resumeaddEventListener("pause", …) in productiontype="audio/mpeg" for MP3 sources.pause() the same as user pausealert() when media pausespause with endedonpause on non-media elementsThe 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.
onpauseBookmark these before building custom media players.
audio, video.
ScopePair both.
PatternState check.
APISave spot.
UXMid-stream.
Gotchapause event fires — when audio or video playback is paused.<audio> and <video> — HTML5 media elements only.onended when media finishes. onpause is for mid-playback pauses.pause event.media.addEventListener("pause", handler) is preferred for production code.Practice the onpause attribute with audio pause examples in the Try It editor.
5 people found this page helpful