A data-* attribute on a <video> tag attaches additional data to the video control.
To create a custom attribute, replace * with a lowercase string, such as data-id
, data-status
, or data-location
.
A custom data-title
attribute on a <video>.
The attribute value is not visible, but is readable by JavaScript.
<video data-title="Bucky Bunny" width="320" height="240" controls>
<source src="/media/movie.mp4" type="video/mp4">
<source src="/media/movie.ogg" type="video/ogg">
</video>
The data-* attribute adds custom information to a <video> element.
The * part is replaced with a lowercase string, such as data-id, data-volume, data-breed, etc.
An <video> 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 video control.
<video data-*="value">
Note: The * can be any string, such as data-id
, data-cost
, data-supplier
, etc.
Value | Description |
---|---|
value | A string value. Can be numeric, alphanumeric, JSON, etc. |
A data-rating
attribute on a <video> tag.
Clicking the button will display the rating for the video.
<video id="myvideo" data-rating="All audiences" width="320" height="240" controls>
<source src="/media/movie.mp4" type="video/mp4">
<source src="/media/movie.ogg" type="video/ogg">
</video>
<br/><br/>
<button onclick="show();">Show data</button>
<script>
let show = () => {
let element = document.getElementById("myvideo");
alert("Rating = " + element.getAttribute('data-rating'));
}
</script>
The <video> tag contains a custom data-rating
attribute.
The data-rating
value stores the title of the video file.
Button clicks are handled by the onclick
event.
Onclick invokes a JavaScript function that extracts and displays the <video> rating.
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.5 | Mar 2010 |
Safari
|
4.0 | Jun 2009 |
Back to <video>