References

Beginner-friendly references for web development, with live, editable examples.

The HTML onplaying event

Event All modern browsers Updated
Quick answer

The HTML onplaying attribute runs JavaScript when media is actually playing after being paused or buffering. It is an inline handler for the playing event; in modern code prefer addEventListener('playing', …).

Overview

The onplaying event attribute runs JavaScript when media begins playing after a stall. In JavaScript the event itself is named playing — drop the on prefix when you call addEventListener.

It is one of the media events, fired by <audio> and <video> elements as their loading and playback state changes. These events drive custom players — progress bars, buffering spinners, play/pause UI.

You can wire this up with the inline onplaying HTML attribute, but the modern, recommended approach is element.addEventListener('playing', handler) in JavaScript. That keeps behavior out of your markup, lets you attach several handlers to the same event, and makes them easy to remove. The inline attribute is fine for quick demos.

Syntax

<element onplaying="handler()">…</element>

element.addEventListener('playing', handler);

Best practices

  • Prefer element.addEventListener('playing', handler) over the inline onplaying attribute — it separates behavior from markup and allows multiple handlers.
  • Attach media events to the <video>/<audio> element to build custom player UI.
  • Read the element's state (currentTime, duration, buffered) inside the handler.
  • Still provide native controls and captions for accessibility.

Frequently asked questions

What is the onplaying event?
It runs JavaScript when media begins playing after a stall. In JavaScript the event is named playing.
Which elements fire this event?
Media elements — <audio> and <video> — as their playback or loading state changes.
How do I build a custom video player?
Listen to the media events and read/set properties like currentTime and paused on the element to drive your own controls.
Should I use the onplaying attribute or addEventListener?
Prefer addEventListener('playing', …) in JavaScript. The inline onplaying attribute works but mixes behavior into the markup and allows only one handler per element.