Programming
Webdesign
HTML5 data-* attributes are great and you know it | HTML5 data-* attributes are great and you know it |
|
Like I said in my previous article, all information about HTML5 is way too big to put into one blog article. We first looked at what HTML5 microdata can do for us, and today we'll dive into another feature W3C added to their HTML specification. It's called custom data attributes (by developers mostly referenced as
The Uses of technologies Chris Coyier created this useful overview for the correct use of the several webdesign technologies:
Now let's focus on two techniques Chris points out here: HTML and JavaScript. Keep their appropriate use in mind, since it's very important to know the difference. JavaScript plugins JavaScript plugins are great. jQuery has loads of official jQuery plugins which extend the capabilities of the framework just that little bit further (I wrote some plugins for jQuery too). Since you want the plugin to be used by loads of people, you make it easy to use and as flexible as possible. Although jQuery allows options to be used to change the behaviour of the plugin, sometimes you need to put data inside the HTML for it to make it work. For example, take a look at this piece of code:
<img class="pic-0" /> <img class="pic-1" /> <img class="pic-2" />
for(var i = 0; i < $("img").length; i++) { $(".pic-" + i).doStuff(); // Do something }
I know this isn't the best example I came up with, but that isn't the goal of this article. The plugin loops through all the elements starting with the The main problem here, is the mis-use of the
<p> This is the actual text <span>This is the tooltip</span> </p>
$("p span").hide(); $("p").hover(function() { $("span", this).show(); });
So here we "hide" the information stored in the tooltip inside a Once again, we mis-use something here. This time, it's the The jQuery solution For these kind of problems, jQuery already has a pretty nifty solution. Since we don't want our HTML-elements or attributes to be mis-used, we can dynamically add data to the DOM using the .data() API. This allows jQuery developers to add the data they want to the HTML elements, without abusing the HTML itself. Let's take a look at how the two previous examples could be re-written using the
<img /> <img /> <img />
var i = 0; $("img").each(function(){ $(this).data("iterator", i); // Store data i++; }); $("img").hover(function(){ alert($(this).data("iterator")); // Retrieve data });
Did you see that? We don't have any dirty But now for the tricky part: How would you re-write the tooltip example to use the Bring in the data-* The custom data attributes give us a very clean solution for this problem. We can now store any data and add it to the HTML as an attribute, as long as it starts with
<img data-index="0" /> <img data-index="1" /> <img data-index="2" /> <p data-tooltip="This is the tooltip"> This is the actual text </p>
I know the first example looks very silly, but I'll come to that later. The second one, on the other hand, is pretty cool! We now have the appropriate place to store that kind of information. Another upside, is that it's hidden by default and we can easily retrieve it using JavaScript to display the tooltip. When using jQuery, you could simply retrieve the value using the
var index = $("img").attr("data-index"); var tooltip = $("p").attr("data-tooltip"); Take note the But now back to the images. Although the
<img alt="My alt text" data-longdescr="My long description" data-geo="Location I took the picture" data-cam="The type of camera I used" data-time="The time I took the picture" />
Did you see that? I'm suddenly capable of adding loads and loads of more data to one single image. Imagine how you would write this when you couldn't use the custom data attributes? Logically, it probably wouldn't make as much sense as above. Now you can use these attributes to display them using JavaScript. This is the kind of power you get when using custom data attributes. Conclusion In my opinion, the What do you think? Have you ever encountered a problem like described above? If so, do you like the Further reading Tags: html5 data custom attribute tutorial Interested in this topic? You might enjoy another article I've written called
Did you like this article? Subscribe to my feed or email to keep updated on new articles. Spread the word and submit to:![]() ![]() ![]() ![]() ![]()
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| < Prev | Next > |
|---|
| Subscribe | ||
|---|---|---|
|
| Search |
|---|
| Or try the sitemap |










