HTML <audio> data-* Attribute

The data-* attribute on an <audio> tag attaches additional data to the audio player. To create a custom attribute, replace the * with a lowercase string, such as data-id, data-status, or data-location.

Example

#

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">
</audio>

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.


Syntax

<audio data-*="value">

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


Values

#

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">
</audio>

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

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

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:

Chrome
4.0 Jan 2010
Firefox
3.5 Jun 2009
IE/Edge
9.0 Sep 2012
Opera
10.0 Aug 2009
Safari
11.5 Jun 2009

You may also like

 Back to <audio>
Guides