HTML5 Data Attribute: This data attribute gives the potential of binding specific information to particular section. Information when attached can be called via JavaScript rather than calling this from server side Ajax or database calls. One can also apply specific CSS to get the data being presented in custom way. Data attributes can be applied to all elements.
— Where * can be any custom string value to match your content information
Here is active example for using data attribute with JavaScript:
Species
Click on a species to see what type it is:
As per usage value can be anything string, must be one character long after prefix data*- and uppercase are not allowed at all.
You can easily read these attributes with JavaScript as well to use them further. With getAttribute and setAttribute you can get the value of these data attributes. In the above example we have used JavaScript to get the browser notification as you click on the line.
var animalType = animal.getAttribute("data-animal");
These Data attributes are part of HTML attributes only and you can apply CSS on them on present them to match your site design. Here is quick example of using CSS Selector in Data Attribute to change the layout of the design.
li[data-animal="bird"] {background: #8BC34A;} li[data-animal="fish"] {background: red;} li[data-animal="spider"] {background: yellow;}
All tags and attributes in HTML are case insensitive but it is recommended them to use them in lowercase from the beginning.
These data attributes if used in capital gets lowered ASCII-lowercased automatically so the restriction doesn’t affect documents at all. Though modern browsers will rectify these can cause error in the markup so avoid using them in capitalise.
Also Check Out
When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…
In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…
In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…
In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…
When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…
In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…