Loading...

HTML essentialsAdvanced

HTML with APIs & data attributes

7 min lesson

Next
  • HTML pages can talk to browser APIs (built-in features) and can store extra data in data attributes.
  • A data attribute is a custom attribute that starts with data-, like data-id="42", used by your scripts.
  • Browser APIs include localStorage, fetch, geolocation and many more, all reached through JavaScript.
  • Think of data attributes as sticky notes on elements, and APIs as power sockets in the wall that your script plugs into.
  • data-* values are strings, so convert numbers with Number() before doing math.
  • This builds on Best practices & validation: your markup stays valid while carrying extra data.
  • The example stores a product id on a button and reads it in a script; you will see a message with the id.
  • Next, Template, slot & custom elements reuses HTML instead of writing it again.
Keep in mind
  • Use data-* only for your own data; never invent non-standard attributes.
  • Read data attributes with element.dataset, not getAttribute, when possible.
  • Keep data small; big data belongs in JSON and a request.
  • Remember every data-* value arrives as a string.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Data attributes</title>
</head>
<body>
  <!-- A button that carries a product id as data -->
  <button id="buy" data-product-id="42" data-price="250">Buy</button>

  <!-- A small output area for the message -->
  <p id="msg">No action yet.</p>

  <script>
    // Read the data attributes when the button is clicked
    document.getElementById('buy').addEventListener('click', function () {
      const id = this.dataset.productId;      // string "42"
      const price = Number(this.dataset.price); // convert to a number
      // Show the id and price on the page
      document.getElementById('msg').textContent = `Product ${id} costs ${price}.`;
    });
  </script>
</body>
</html>

What happens: A page with the button "Buy" and the text "No action yet." Clicking the button replaces the text with "Product 42 costs 250."

Breaking it down

data-product-id

data-product-id="42"

Stores the product id in the HTML. Names must start with data- and use lowercase letters and dashes.

dataset

this.dataset.productId

Reads data attributes in camelCase. data-product-id becomes dataset.productId.

Number()

Number(this.dataset.price)

Converts the string value to a number, so math and comparisons work correctly.

Common browser APIs

APIUse for
localStorageSaving small data in the browser
fetchRequesting data from a server
geolocationGetting the user's location
IntersectionObserverDetecting when elements scroll into view
ClipboardReading and writing the clipboard

A helpful analogy

Data attributes are sticky notes stuck to a box: the box looks the same, but your script reads the note to learn what is inside. APIs are the power sockets in the wall; your script plugs into them when it needs to do more.

Nesting rules

  • 1.Data attribute names must be lowercase and may contain letters, digits and dashes.
  • 2.Never store sensitive data like passwords in data attributes; anyone can read them.

Common mistakes

Forgetting to convert to a number

Avoid

this.dataset.price + 10

Use instead

Number(this.dataset.price) + 10

Strings concatenate instead of adding. "250" + 10 becomes "25010", not 260.

Inventing a non-standard attribute

Avoid

<button productid="42">

Use instead

<button data-product-id="42">

data-* is the only safe way to add custom attributes; other names may be ignored or break validation.

Quick tips

  • •Use DevTools to inspect an element and see all its data attributes in the properties panel.
  • •Keep data attributes small; for large data, fetch JSON from the server.
Previous
Next