HTML

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:

<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

Recent Posts

Unlocking the Secrets of JSON.stringify(): More Than Meets the Eye

JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development. At…

2 months ago

How to Handle AJAX GET/POST Requests in WordPress

AJAX (Asynchronous JavaScript and XML) is a powerful technique used in modern web development that…

3 months ago

Page Speed Optimization: Post-Optimization Dos and Don’ts

Introduction After successfully optimizing your website for speed, it's essential to maintain and build upon…

3 months ago

Ultimate Guide to Securing WordPress Folders: Protect Your Site from Unauthorized Access

Securing your WordPress folders is crucial to safeguarding your website from unauthorized access and potential…

4 months ago

HTML CSS PHP File Upload With Circle Progress Bar

Creating a file upload feature with a circular progress bar involves multiple steps. You'll need…

4 months ago

Using WP Rocket with AWS CloudFront CDN

Integrating WP Rocket with AWS CloudFront CDN helps to optimize and deliver your website content…

4 months ago