HTML <audio> data-* Attribute

A data-* attribute on an <audio> tag attaches additional data to the audio player.

To create a custom attribute, replace * with a lowercase string, such as data-id, data-status, or data-location.



A custom data-composer attribute on an audio player.
The attribute value is not visible, but is readable by JavaScript.

<audio data-composer="J.A. Hollister" controls>
  <source src="/media/epic.mp3" type="audio/mpeg">
  <source src="/media/epic.wav" type="audio/wav">

Using data-*

The data-* attribute adds custom information to an <audio> element.

The * part is replaced with a lowercase string, such as data-id, data-volume, data-breed, etc.

An <audio> element can have any number of data-* attributes, each with their own name.

Using data-* attributes reduces the need for requests to the server.

Note: The data-* attribute is not visible and does not change the appearance of the audio control.


<audio data-*="value">

Note: The * can be any string, such as data-iddata-costdata-supplier,  etc.



Value Description
value A string value. Can be numeric, alphanumeric, JSON, etc.

More Examples

A custom data-producer attribute on an <audio> element.
Clicking the button will display the producer value.

<audio data-producer="Philadelphia Harmonic" controls id="myaudio">
  <source src="/media/epic.mp3" type="audio/mpeg">
  <source src="/media/epic.wav" type="audio/wav">

<button onclick="show();">Show data</button>

  let show = () => {
     let element = document.getElementById("myaudio");
     alert("Producer = " + element.getAttribute('data-producer'));

Code explanation

The <audio> tag contains a custom data-producer attribute.

The data-producer value is the producer of the audio file.

Button clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the audio producer.

Browser support

Here is when data-* support started for each browser:

4.0 Jan 2010
3.5 Jun 2009
9.0 Sep 2012
10.0 Aug 2009
11.5 Jun 2009

You may also like

 Back to <audio>

Last updated on Sep 30, 2023