Data attributes provide the ability to tag HTML markup with additional layers of data. An element can have any number of data attributes you want.Here's an example using a list item to store data for a user:Of course, this data isn't very useful to a visitor because they can't actually see it, but it's wildly usef… They look like this: < Thankfully, this is pretty much all of them. For ex: I have a span element which displays a text as ‘Employee name is John! Both JavaScript and jQuery work equally well with data attributes. Also what is the best way to test support for this? Number values must be quoted in the selector for the styling to take effect. When we access these elements from javascript, the adequate information stored will make our work easy. To support IE 10 and under you need to access data attributes with getAttribute() instead. @jpvincent – Yeah, I totally agree with you. http://lists.w3.org/Archives/Public/public-html-comments/, http://www.codeproject.com/KB/scripting/XHTML-CSS-Data-Attributes.aspx, Computer says NO to HTML5 document outline, On HTML belts and ARIA braces (The Default Implicit ARIA semantics they didn’t want you to know about), HTML5 – Check it Before you Wreck it with Mike[tm] Smith, Creative Commons Attribution-Non-Commercial 2.0, To store the initial height or opacity of an element which might be required in later JavaScript animation calculations, To store parameters for a Flash movie that’s loaded via JavaScript, To store custom web analytics tagging data as demonstrated by, To store data about the health, ammo, or lives of an element in a JavaScript game. This article was written by Chris Bewick. For instance, let’s say you have a list of different restaurants on a webpage. @Scott – the namespacing isn’t to create “site-specific” data attributes, but in-application namespacing. I guess the initial health and ammo data could be stored in a database and using a data-attribute would be a valid mechanism to transfer this information to the game’s javascript. Although you can’t utilise the new JavaScript APIs just yet, you can enjoy great success using getAttribute and setAttribute safe in the knowledge that they will work in all major browsers. These new custom data attributes consist of two parts: Using this syntax, we can add application data to our markup as shown below: We can now use this stored data in our site’s JavaScript to create a richer, more engaging user experience. I have a hidden secret! Troubleshooting JavaScript, Storing the information you need — Variables, Basic math in JavaScript — Numbers and operators, Making decisions in your code — Conditionals, Assessment: Adding features to our bouncing balls demo, General asynchronous programming concepts, Cooperative asynchronous JavaScript: Timeouts and intervals, Graceful asynchronous programming with Promises, Making asynchronous programming easier with async and await, CSS property compatibility table for form controls, CSS and JavaScript accessibility best practices, Assessment: Accessibility troubleshooting, React interactivity: Editing, filtering, conditional rendering, Ember interactivity: Events, classes and state, Ember Interactivity: Footer functionality, conditional rendering, Adding a new todo form: Vue events, methods, and models, Vue conditional rendering: editing existing todos, Dynamic behavior in Svelte: working with variables and props, Advanced Svelte: Reactivity, lifecycle, accessibility, Setting up your own test automation environment, Tutorial Part 2: Creating a skeleton website, Tutorial Part 6: Generic list and detail views, Tutorial Part 8: User authentication and permissions, Tutorial Part 10: Testing a Django web application, Tutorial Part 11: Deploying Django to production, Express Web Framework (Node.js/JavaScript) overview, Setting up a Node (Express) development environment, Express tutorial: The Local Library website, Express Tutorial Part 2: Creating a skeleton website, Express Tutorial Part 3: Using a database (with Mongoose), Express Tutorial Part 4: Routes and controllers, Express Tutorial Part 5: Displaying library data, Express Tutorial Part 6: Working with forms, Express Tutorial Part 7: Deploying to production, Custom attributes are also supported in SVG 2; see. Element.prototype.__defineGetter__(‘dataset’, function(){ This dataset property — part of the new HTML5 JavaScript APIs — will return a DOMStringMap object of all the selected element's data- attributes. if someone write : setAttribute has been specified as capable since DOM Core 1.0 to set user defined attributes and there are no implementation issues. I’m glad I did today, thanks for this really helpful article. See this screencast for an example using generated content and CSS transitions (JSBin example). data-html5doctor-height or data-my-plugin-height. This code also partially supports the setting of data attributes, but it will only store the new attribute values within the JavaScript and will not update the DOM element as a full, native implementation of the dataset property would. What about expando properties? The attribute names are read by DataTables and used, potentially in combination with, the standard Javascript initialisation options (with the data-* attributes … If you are planning to have data understood by the search engines, you could use microdata which is more semantic. if (attr.name && attr.name.indexOf('data-') == 0) { Although this code is mainly a proof of concept, it may be useful for mobile application or intranet development in closed environments where cross-browser (IE) compatibility is not an issue. The data-* attributes can be used to define our own custom data attributes. Example: if step="3", legal numbers could be -3, 0, 3, 6, etc. My take is that if you’re creating classes that will never be styled, or storing variables in hidden form fields that never get read from on the server, those are both good use cases for the data- attribute. That said, though, for custom element-associated metadata, they are a great solution. data-field_type=”text” could be used for both validation and styling layout. period… simple right? HTML5 data attributes are supported in all the modern web browsers including Google Chrome, Mozilla Firefox, and Apple Safari. Thank you, I like to come in here again and again. To access a particular data attribute, reference it by name without the "data-" prefix. This gave me a good overview of the possibilities and things to come in the specs. Thanks for the explanation of the data- attributes. Data attributes should not be used if there is a existing attribute or element which is more appropriate for storing your data. I use the data attributes a lot for jQuery apps. While HTML5 isn’t either, these data-attributes are perfectly fine from a SGML point of view. Using orthogonal data for searching allows both forms of the telephone number to be used, while only the nicely formatted number is displayed in the table. The main issues to consider are Internet Explorer support and performance. This sometimes led to problems and could cause conflicts between the styling and functionality of websites. My extension allows developers to save bandwidth by using custom data attributes so that any user with the extension installed will instead load common JavaScript frameworks (e.g. Nice article, perhaps it needs a slight modification to be understandable by some of the very fresh and new entries into JS and HTML5. The Web Evolved: A new book on HTML5 & CSS3 by Richard Clark, Oli Studholme, Christopher Murphy and Divya Manian. Expando properties and “data-*” attributes aren’t quite the same thing. /* Show the ascending arrow */ All rights reserved. But what are the support for html5 doctype? A new feature being introduced in HTML 5 is the addition of custom data attributes. jQuery, Prototype, etc.) I don’t understand why this article quotes the part of the spec saying data-* attributes are site-specific, and then gives a warning promoting namespacing. Carrots .