HTML <object> data-* Attribute

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

Example

#

A data-content attribute on an <object> element.

PDF cannot be displayed.
<object data-content="Legal file" data="/media/sample.pdf" 
        type="application/pdf" style="width:100%;height:500px;">
   PDF cannot be displayed.
</object>

Using data-*

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

The * part is replaced with a lowercase string, such as data-id, data-cost, or data-location.

An <object> 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 object.


Syntax

<object data-*="value">

Note: The * part can be any string, for example data-id, data-cost, data-supplier, etc.

Values

#

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

More Examples

An <object> tag with custom data-content attribute.
Clicking the button will display the content of the pdf document.

PDF cannot be displayed.

<object data-content="Legal brief" id="myobject"
        data="/media/sample.pdf" type="application/pdf"
        style="width:100%;height:450px;">
   PDF cannot be displayed.
</object>

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

<script>
  let show = () => {
    let element = document.getElementById("myobject");
    alert("Content = " + element.getAttribute('data-content'));
  }
</script>

Code explanation

The <object> tag has a custom data-filename attribute.

The data-filename attribute stores the resource filename of the embedded object.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the <object> file name.

Note: Notice how the file name displays immediately without server call.


Browser support

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

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <object>
Guides