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

The onplay attribute is an inline event handler that runs JavaScript when the play event fires on an audio or video element. It runs when playback starts or resumes — by the user clicking play, pressing space, or when your code calls element.play(). Use it to show a “now playing” indicator, start animations, pause other media, or log analytics. Pair with onpause when playback stops. Prefer addEventListener("play", …) in production.
Inline JS.
Starts / resumes.
Media only.
Programmatic.
Full UX pair.
Preferred way.
onplay AttributeThe primary purpose of onplay is to react when media playback begins — so your page can update custom controls, show progress UI, sync lyrics or captions, pause background audio, or track that the user started listening or watching.
The play event fires on the media element when playback is initiated. It also fires when resuming after a pause. It is related to but not identical to playing, which fires once media is actually rendering frames or sound after buffering.
Calling video.play() in JavaScript also fires the play event — your handler runs either way.
Set onplay on audio or video, or assign media.onplay in JavaScript:
<audio controls onplay="startPlayback()" onpause="stopPlayback()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
media.onplay = startPlayback;
</script>play event fires.<audio> and <video> elements.element.onplay in script.onpause to update UI when playback stops.media.addEventListener("play", handler).input or div.The onplay attribute accepts a string of JavaScript code:
onplay="startPlayback()" — Call a named function.onplay="status.textContent = 'Playing'" — Inline statement.audio.onplay = () => { … } — property assignment.function startPlayback() {
status.textContent = "Now playing";
status.className = "status-playing";
indicator.hidden = false;
}
audio.addEventListener("play", startPlayback);
audio.addEventListener("pause", stopPlayback);| Property / event | When it fires | Notes |
|---|---|---|
play | Playback starts / resumes | onplay |
pause | Playback paused | onpause |
playing | After buffering | onplaying |
media.play() | Start via script | Also fires play |
media.paused | Any time (read property) | false when playing |
| Element | Supported? | Notes |
|---|---|---|
<audio> | Yes | Primary use case |
<video> | Yes | Same play event |
<input>, <div> | No | Not media elements |
<iframe> (embed) | No | Cross-origin limits |
| Custom play button | Related | Call .play() on media |
onplay vs onpause vs onplaying| Attribute | Event | Typical use |
|---|---|---|
onplay | play | Show playing state, start sync |
onpause | pause | Hide indicator, save position |
onplaying | playing | After data is ready |
onended | ended | Next track, replay |
Inline audio handler, addEventListener assignment, and custom play button.
Play and pause the audio — status updates without an alert:
Ready — press play
Show feedback when playback starts:
<audio controls onplay="startPlayback()" onpause="stopPlayback()">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="status">Ready</p>
<script>
function startPlayback() {
document.getElementById("status").textContent = "Playing";
}
function stopPlayback() {
document.getElementById("status").textContent = "Paused";
}
</script>The browser fires play on the audio element when playback starts or resumes. The inline attribute runs startPlayback() at that moment.
Attach the handler with addEventListener:
<audio controls id="myAudio">
<source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<script>
document.getElementById("myAudio").addEventListener("play", function () {
document.getElementById("log").textContent =
"Playback started!";
});
</script>addEventListener("play", …) is the preferred way to register the same handler as inline onplay.
Call audio.play() — the play event still fires:
playBtn.addEventListener("click", function () {
audio.play(); // triggers play event → onplay / listener runs
});
audio.addEventListener("play", function () {
status.textContent = "Now playing via custom button";
});Custom players hide native controls but still rely on the media element’s play event for UI sync.
aria-label.aria-live="polite" on status regions.Paused / ready.
Click or .play().
onplay runs.
Now playing.
The play event and onplay handler are supported wherever HTML5 audio and video are supported — all modern browsers.
onplay supportAll major browsers fire play on audio and video elements.
Bottom line: Reliable for media play UX in all modern browsers.
onplay with onpauseaddEventListener("play", …) in production.play() promise rejections (autoplay policy)type="audio/mpeg" for MP3 sourcesalert() when media startsplay with playing eventsonplay on non-media elementsplaying tooThe onplay attribute runs JavaScript when audio or video playback starts or resumes — essential for custom players, status UI, and synced page effects.
Pair it with onpause, handle programmatic .play(), and prefer addEventListener in production code.
onplayBookmark these before building custom media players.
audio, video.
ScopePair both.
PatternScript too.
APIAfter pause.
UXSee onplaying.
Gotchaplay event fires — when audio or video playback starts or resumes.<audio> and <video> — HTML5 media elements only.play event fires both on first play and when resuming from a paused state.play fires when playback is requested. playing fires when media is actually outputting after buffering.media.addEventListener("play", handler) is preferred for production code.Practice the onplay attribute with audio play examples in the Try It editor.
5 people found this page helpful