HTML onemptied Attribute

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

Definition and Usage

The onemptied attribute is an event-handler attribute for <audio> and <video>. It runs JavaScript when the emptied event fires — the media has become empty.

MDN’s classic case: media was already loaded (or partially loaded), then you call media.load() to reload it. Use it to reset UI (status text, progress, posters) before a new fetch starts with onloadstart.

Baseline · Widely available.

Not deprecated and not experimental. Per MDN, emptied is not cancelable and does not bubble. Stable since about July 2015.

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer media.addEventListener("emptied", handler) in production; use inline onemptied for quick demos and learning.

HTML Syntax

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

script is JavaScript that runs when media becomes empty. Equivalent JS: media.onemptied = handler or media.addEventListener("emptied", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when media becomes emptyTry it
onemptied="fn()"Call a named function from the attributeTry it
addEventListener("emptied")Preferred modern listener on the media elementTry it
vs loadstartEmpty reset first, then a new load beginsTry it

Live Preview

Play or wait for the clip to load, then click Reload (load()). emptied fires when the element is reset.

Media

Sample media from /video/count.mp4 or /audio/count.mp3.

Player

Waiting — click Reload after media has started loading.

HTML Examples

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

Example 1 — emptied After load()

Call load() on already-loaded media to trigger emptied.

HTML
<audio id="clip" controls onemptied="onEmpty()">
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<button type="button" id="reload">Reload</button>
<p id="status">Ready</p>

<script>
  function onEmpty() {
    document.getElementById("status").textContent =
      "emptied — media was reset.";
  }

  document.getElementById("reload").addEventListener("click", function () {
    document.getElementById("clip").load();
  });
</script>
Try It Yourself

How It Works

  1. Media loads (or starts loading) into the element.
  2. You call load() — the resource selection algorithm restarts.
  3. emptied fires — the element became empty / was reset.
  4. Update UI — clear “playing” / progress labels if needed.

Example 2 — Change Source Then load()

Update src, call load(), and listen with addEventListener.

HTML
<video id="clip" width="400" height="225" controls>
  <source id="src" src="/video/count.mp4" type="video/mp4">
</video>
<button type="button" id="switch">Reload source</button>
<p id="log">Waiting…</p>

<script>
  var clip = document.getElementById("clip");
  var src = document.getElementById("src");
  var log = document.getElementById("log");
  var count = 0;

  clip.addEventListener("emptied", function () {
    count += 1;
    log.textContent = "emptied #" + count + " — element reset.";
  });

  document.getElementById("switch").addEventListener("click", function () {
    src.src = "/video/count.mp4";
    clip.load();
  });
</script>
Try It Yourself

How It Works

  1. Change src on the nested <source> (or on the media element).
  2. Call load() so the browser applies the new selection.
  3. emptied reports the reset before the new fetch completes.
  4. Count events — useful in playlists / source switchers.

Example 3 — emptied Then loadstart

Log both events on a reload to see the usual order.

HTML
<audio id="clip" controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
</audio>
<button type="button" id="reload">Reload</button>
<ul id="log"></ul>

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

  function add(line) {
    var li = document.createElement("li");
    li.textContent = line;
    log.appendChild(li);
  }

  clip.addEventListener("emptied", function () {
    add("emptied — media became empty");
  });

  clip.addEventListener("loadstart", function () {
    add("loadstart — looking for media");
  });

  document.getElementById("reload").addEventListener("click", function () {
    clip.load();
  });
</script>
Try It Yourself

How It Works

  1. emptied — previous resource state was cleared.
  2. loadstart — a new load begins looking for data.
  3. Reset UI on emptied — start “loading…” UI on loadstart.
  4. Not the same as abort — onabort is for a fetch that was aborted.

Edge Cases & Pitfalls

Most confusion comes from expecting emptied on first load or from missing load().

trigger

Often needs a reload

MDN’s example is calling load() after media was already (partially) loaded. First paint alone may not fire emptied.

api

Not cancelable

You cannot stop the empty reset with preventDefault(). React in the handler; do not try to cancel it.

order

emptied ≠ loadstart

Reset first (emptied), then the new fetch may start (loadstart). Put “clear UI” in emptied and “loading…” in loadstart.

target

Listen on audio/video, not source

The event targets the media element. Nested <source> tags do not receive onemptied.

src

Changing src alone may not be enough

After updating a nested <source>, call media.load() so the browser restarts resource selection.

Browser Support

onemptied 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

onemptied

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

97% Global support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
onemptied Widely supported

Bottom line: Safe to use for reset/reload UI. Prefer addEventListener('emptied') in apps, and pair with loadstart for loading feedback.

Key Takeaways

  • Role: media became empty — often after load() on loaded media.
  • Not cancelable and does not bubble.
  • Order: emptied (reset) → often loadstart (new fetch).
  • UI tip: clear progress/status on emptied; show loading on loadstart.

One line: onemptied means the media element was emptied — classic trigger is load() on already-loaded media.

Frequently Asked Questions

It is an event-handler attribute on audio or video. When the media has become empty, the emptied event fires and the JavaScript in onemptied runs — for example after calling load() to reload already-loaded media.
The HTML audio and video elements. Put the handler on the media element itself, not on nested source tags.
When media becomes empty. A common case from MDN: media was already loaded (or partially loaded), then load() is called to reload it. Changing src and calling load() can also reset the element.
No. Per MDN, emptied is not cancelable and does not bubble. You cannot stop the reset with preventDefault().
emptied means the element was cleared / became empty. loadstart means the browser begins looking for media data for a new load. On a reload you often see emptied, then loadstart.
No. The emptied event and onemptied handler are standard HTML media features and Baseline widely available since about July 2015.

Next: onended

Run JavaScript when audio or video playback reaches the end.

onended 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