HTML <object> data-* Attribute

A data-* attribute on an <object> tag attaches additional data to the object element.

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



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.

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.


<object data-*="value">

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



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"
   PDF cannot be displayed.

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

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

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:

1.0 Sep 2008
1.0 Sep 2002
1.0 Aug 1995
1.0 Jan 2006
1.0 Jan 2003

You may also like

 Back to <object>

Last updated on Sep 30, 2023