Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

HTML <li> data-* Attribute

A data-* attribute on a <li> tag attaches additional data to the list item.

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

Example

#

A custom data-salary attribute on a list item.
The attribute value is not visible.

  1. Designers
  2. Developers
  3. Managers
<ol>
  <li data-salary="$3,193">Designers</li>
  <li data-salary="$76,404">Developers</li>
  <li data-salary="$86,139">Managers</li>
</ol>

Using data-*

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

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

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


Syntax

<li 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-salary attribute on three <li> tags.
Clicking an item will display the salary value.

  1. Designers
  2. Developers
  3. Managers
<ol>
  <li data-salary="$63,193" onclick="show(this);">Designers</li>
  <li data-salary="$76,404" onclick="show(this);">Developers</li>
  <li data-salary="$86,139" onclick="show(this);">Managers</li>
</ol>

<script>
  let show = element => {
     alert("Salary = " + element.getAttribute('data-salary'));
  }
</script>

Code explanation

Three list items are created using the <li> tag.

Each has a custom data-salary attribute that indicates the salary for each job position.

Clicks are handled by the onclick event.

Onclick invokes a JavaScript function that extracts and displays the list item salary.

Note: Notice how the salary 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 <li>
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest self-service freelancing marketplace for people like you.

Guides