Recently I have been reading more and more about people using custom attributes in their HTML tags, mainly for the purpose of embedding some extra bits of data for use in javascript code.
I was hoping to gather some feedback on whether or not using custom attributes is a good practice, and also what some alternatives are.
It seems like it can really simplify both server side and client side code, but it also isn't W3C compliant.
Should we be making use of custom HTML attributes in our web apps? Why or why not?
For those who think custom attributes are a good thing: what are some things to keep in mind when using them?
For those who think custom attributes are bad thing: what alternatives do you use to accomplish something similar?
Update: I'm mostly interested in the reasoning behind the various methods, as well as points as to why one method is better than another. I think we can all come up with 4-5 different ways to accomplish the same thing. (hidden elements, inline scripts, extra classes, parsing info from ids, etc).
Update 2: It seems that the HTML 5 data-
attribute feature has a lot of support here (and I tend to agree, it looks like a solid option). So far I haven't seen much in the way of rebuttals for this suggestion. Are there any issues/pitfalls to worry about using this approach? Or is it simply a 'harmless' invalidation of the current W3C specs?
For complex web apps, I drop custom attributes all over the place.
For more public facing pages I use the "rel" attribute and dump all my data there in JSON and then decode it with MooTools or jQuery:
I'm trying to stick with HTML 5 data attribute lately just to "prepare", but it hasn't come naturally yet.
Here's a technique I've been using recently:
The comment-object ties to the parent element (i.e. #someelement).
Here's the parser: http://pastie.org/511358
To get the data for any particular element simply call
parseData
with a reference to that element passed as the only argument:It can be more succinct than that:
Access it:
The only disadvantage of using this is that it cannot be used with self-closing elements (e.g.
<img/>
), since the comments must be within the element to be considered as that element's data.EDIT:
Notable benefits of this technique:
Here's the parser code (copied from the http://pastie.org/511358 hyperlink above, in case it ever becomes unavailable on pastie.org):
Nay. Try something like this instead:
HTML 5 explicitly allows custom attributes that begin with
data
. So, for example,<p data-date-changed="Jan 24 5:23 p.m.">Hello</p>
is valid. Since it's officially supported by a standard, I think this is the best option for custom attributes. And it doesn't require you to overload other attributes with hacks, so your HTML can stay semantic.Source: http://www.w3.org/TR/html5/dom.html#embedding-custom-non-visible-data-with-the-data-*-attributes
You can create any attribute if you specify a schema for your page.
For example:
Addthis
Facebook (even tags)
I know people are against it, but I came up with a super short solution for this. If you want to use a custom attribute like "mine" so for example:
Then you can run this code to get an object back just like jquery.data() does.