HTML <ul> data-* Attribute

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

Example

#

A custom data-title attribute on a <ul> tag.
The attribute value is not visible, but is readable by JavaScript.

  • Vincent Van Gogh
  • Paul Cézanne
  • Claude Monet
<ul data-title="List of painters">
  <li>Vincent Van Gogh</li>
  <li>Paul Cézanne</li>
  <li>Claude Monet</li>
</ul>

Using data-*

The data-* attribute adds custom information to a <ul> element.

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

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


Syntax

<ul 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-count attribute on a <ul> element.
Clicking the button will display the count value.

  • Vincent Van Gogh
  • Paul Cézanne
  • Claude Monet

<ul id="myul" data-count="3">
  <li>Vincent Van Gogh</li>
  <li>Paul Cézanne</li>
  <li>Claude Monet</li>
</ul>

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

<script>
  let show = () => {
    let element = document.getElementById("myul");
    alert("# Artists: " + element.getAttribute('data-count'));
  }
</script>

Code explanation

The <ul> tag has a custom data-count attribute.

The data-count attribute specifies the list title.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the number of items inside the list.

Note: Notice how the list title 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 <ul>
Guides