HTML onplay Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onplay attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the play event fires — the media element’s paused property changed from true to false (usually after play(), the Play control, or autoplay).

The event is not cancelable and does not bubble. Pair it with onpause for UI sync. For “actually playing after a stall,” see onplaying.

Baseline · Widely available.

Not deprecated and not experimental. The play event is stable across modern browsers since about July 2015.

Prefer addEventListener in apps.

Inline onplay is fine for learning. Production code usually uses media.addEventListener("play", handler). Autoplay without a user gesture is often blocked unless the media is muted.

HTML Syntax

syntax.html
<video|audio onplay=" script">

script is JavaScript that runs when play fires. Equivalent JS: media.onplay = handler or media.addEventListener("play", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when paused becomes falseTry it
onplay="fn()"Call a named function from the attributeTry it
addEventListener("play")Preferred modern listener on audio/videoTry it
With onpauseUpdate status for both play and pauseTry it
+ controls / play()User Play button or scripted play() triggers playTry it

Live Preview

Press play (or the Play button). Status updates when play and pause fire.

Media

Sample /video/count.mp4 or /audio/count.mp3. Use controls or the Play button.

Player

Waiting — press play

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline onplay on Video

Update status when playback starts.

HTML
<video width="400" height="225" controls onplay="onPlayed()">
  <source src="/video/count.mp4" type="video/mp4">
</video>
<p id="status">Waiting for play…</p>

<script>
  function onPlayed() {
    document.getElementById("status").textContent = "play fired — media is playing.";
  }
</script>
Try It Yourself

How It Works

  1. Put onplay on the media element — here <video>.
  2. User presses Play (or script calls play()).
  3. paused becomes false and the play event fires.
  4. Handler updates UI — status text, icons, analytics, and so on.

Example 2 — addEventListener on Audio

Preferred script pattern on an audio element.

HTML
<audio id="clip" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<p id="log">Idle</p>

<script>
  document.getElementById("clip").addEventListener("play", function () {
    document.getElementById("log").textContent =
      "play event: paused = " + this.paused;
  });
</script>
Try It Yourself

How It Works

  1. Select the audio with getElementById.
  2. Register play with addEventListener.
  3. When play starts, read this.paused (should be false).
  4. Update the log for the student to see.

Example 3 — onplay vs onpause

Keep a simple status line for both start and pause.

HTML
<video id="clip" width="400" height="225" controls>
  <source src="/video/count.mp4" type="video/mp4">
</video>
<p id="state">State: paused</p>

<script>
  var clip = document.getElementById("clip");
  var state = document.getElementById("state");

  clip.addEventListener("play", function () {
    state.textContent = "State: playing";
  });
  clip.addEventListener("pause", function () {
    state.textContent = "State: paused";
  });
</script>
Try It Yourself

How It Works

  1. play — playback requested; update to “playing”.
  2. pause — media paused; update to “paused”.
  3. Same player, two events — cleaner than polling paused.
  4. Need “ready after stall”? Add onplaying as well.

Edge Cases & Pitfalls

Most confusion is play vs playing, or assuming autoplay always succeeds.

vs playing

play ≠ playing

play means paused became false. playing means playback is ready after a pause or buffering delay.

autoplay

Autoplay may be blocked

Browsers often block audible autoplay. Use muted with autoplay, or wait for a user gesture before play().

promise

play() returns a Promise

Handle rejection (policy / load errors). The play event fires only if playback actually leaves the paused state.

bubble

Does not bubble

Listen on the audio/video element itself. Parent listeners will not see a bubbling play.

property

.onplay = replaces listeners

Assigning the IDL property overwrites a previous assignment. Prefer addEventListener for multiple handlers.

Browser Support

onplay is an HTML event-handler attribute for media elements in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onplay

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the play event on audio and video.

97% Global support (approx.)
Google Chrome 3+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 10.5+
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer 9+
Supported
onplay Widely supported

Bottom line: Safe to use for media UI sync. Prefer addEventListener in production, and remember autoplay policies when starting playback without a click.

Key Takeaways

  • Role: run script when media leaves the paused state.
  • Trigger: Play control, play(), or successful autoplay.
  • Pair with: onpause; use onplaying for post-stall readiness.
  • Autoplay: often needs muted or a user gesture.

One line: onplay means “no longer paused” — sync UI, and use onplaying when you care about actual playback readiness.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When the paused property changes from true to false — usually after play() or autoplay — the play event fires and the JavaScript in onplay runs.
The HTML audio and video elements. Put the handler on the media element itself.
onplay means playback was requested and paused became false. onplaying means playback is actually ready to proceed after being paused or delayed for data. They are related but not identical.
onplay runs when playback starts (paused becomes false). onpause runs when playback is paused (paused becomes true). Pair them to keep UI in sync.
Both work. For learning, onplay in HTML is fine. In production code, prefer media.addEventListener("play", handler).
No. The play event and onplay handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onplaying

React when media is ready to play after a pause or lack of data.

onplaying tutorial →

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.

8 people found this page helpful