HTML onplaying Attribute

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

Definition and Usage

The onplaying attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the playing event fires — after playback first starts, and whenever it restarts (for example after a pause, or after a delay due to lack of data).

The event is not cancelable and does not bubble. Use it to hide a loading spinner. Contrast with onplay (paused became false) and onwaiting (stalled for data).

Baseline · Widely available.

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

Prefer addEventListener in apps.

Inline onplaying is fine for learning. Production code usually uses media.addEventListener("playing", handler).

HTML Syntax

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

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

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when playback is ready / resumesTry it
onplaying="fn()"Call a named function from the attributeTry it
addEventListener("playing")Preferred modern listener on audio/videoTry it
With play / waitingCompare request vs ready vs stallTry it
+ controlsUser Play / resume triggers the sequenceTry it

Live Preview

Press play. Status updates for play, playing, and waiting.

Media

Sample /video/count.mp4 or /audio/count.mp3. Local files may not stall — still watch play then playing.

Player

Waiting — press play

HTML Examples

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

Example 1 — Inline onplaying on Video

Mark the UI as “ready” when playback can proceed.

HTML
<video width="400" height="225" controls onplaying="onReady()">
  <source src="/video/count.mp4" type="video/mp4">
</video>
<p id="status">Idle…</p>

<script>
  function onReady() {
    document.getElementById("status").textContent =
      "playing — playback is ready.";
  }
</script>
Try It Yourself

How It Works

  1. Put onplaying on the media element — here <video>.
  2. User presses Play (or playback resumes after a stall).
  3. playing fires when playback can proceed.
  4. Handler updates UI — hide spinner, enable controls, log analytics.

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("playing", function () {
    document.getElementById("log").textContent =
      "playing — ready (paused = " + this.paused + ")";
  });
</script>
Try It Yourself

How It Works

  1. Select the audio with getElementById.
  2. Register playing with addEventListener.
  3. When ready, paused is typically false.
  4. Update the log for the student to see.

Example 3 — play vs playing vs waiting

Log the three events students most often mix up.

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

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

  clip.addEventListener("play", function () {
    state.textContent = "Last event: play (paused became false)";
  });
  clip.addEventListener("playing", function () {
    state.textContent = "Last event: playing (ready / resumed)";
  });
  clip.addEventListener("waiting", function () {
    state.textContent = "Last event: waiting (need more data)";
  });
</script>
Try It Yourself

How It Works

  1. play — playback requested; paused is false.
  2. playing — frames/audio can advance (after start or resume).
  3. waiting — stalled for data; show a loader.
  4. Local short clips may skip a visible waiting — that’s normal.

Edge Cases & Pitfalls

Most confusion is treating play and playing as the same event.

vs play

play ≠ playing

play means paused became false. playing means playback is ready after start, resume, or buffering.

waiting

Pair with waiting

Show a spinner on waiting; hide it on playing. Short local files often never fire waiting.

restart

Fires again on resume

playing fires after first start and whenever playback restarts after pause or lack of data — not only once per page load.

bubble

Does not bubble

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

property

.onplaying = replaces listeners

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

Browser Support

onplaying 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

onplaying

Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support the playing 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
onplaying Widely supported

Bottom line: Safe to use for loading UI. Prefer addEventListener in production, and pair with waiting when you need a buffering indicator.

Key Takeaways

  • Role: run script when playback is ready after start or resume.
  • Trigger: first start, resume after pause, or resume after lack of data.
  • Pair with: onplay for request; onwaiting for stalls.
  • UI tip: hide loaders on playing, not only on play.

One line: onplaying means “playback can proceed now” — ideal for clearing buffering UI after start or a stall.

Frequently Asked Questions

It is an event-handler attribute on audio or video. The playing event fires after playback first starts, and whenever it restarts — for example after a pause or after a delay due to lack of data.
The HTML audio and video elements. Put the handler on the media element itself.
onplay means paused became false (playback was requested). onplaying means playback is actually ready to proceed after start, resume, or buffering. Use onplaying to hide loading UI.
onwaiting fires when playback stops because data is missing. onplaying fires when playback can continue again. Pair them for a loading spinner.
Both work. For learning, onplaying in HTML is fine. In production code, prefer media.addEventListener("playing", handler).
No. The playing event and onplaying handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onpopstate

Handle browser history navigation with the popstate event.

onpopstate 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