HTML 5 Notes For Professional – Chapter – 11 (Data Attributes)

ValueDescription
some valueSpecifies the value of the attribute (as a string)

Older browsers support

Data attributes were introduced in HTML5 which is supported by all modern browsers, but older browsers before HTML5 don’t recognize the data attributes.

However, in HTML specifications, attributes that are not recognized by the browser must be left alone and the browser will simply ignore them when rendering the page.

Web developers have utilized this fact to create non-standard attributes which are any attributes not part of the HTML specifications. For example, the value attribute in the line bellow is considered a non-standard attribute because of the specifications for the <img> tag don’t have a value attribute and it is not a global attribute:

Advertisement
<img src="sample.jpg" value="test" />

This means that although data attributes are not supported in older browsers, they still work and you can set and retrieve them using the same generic JavaScript setAttribute and getAttribute methods, but you cannot use the new dataset property which is only supported in modern browsers.

Data Attribute Use

HTML5 data-* attributes provide a convenient way to store data in HTML elements. The stored data can be read or modified using JavaScript.

<div data-submitted="yes" class="user_profile">
 … some content …
</div>
  • Data attribute structure is data-, i.e. the name of the data attribute comes after the data- part. Using this name, the attribute can be accessed.
  • Data in string format (including json) can be stored using data- attribute
Advertisement

Recent Posts

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago