views:

207

answers:

4

I find it very convenient to set arbitary attributes like:

<a stackoverflowId="123">...</a>

And in JavaScript:

var soId = $selectofA.attr('stackoverflowId'); //jQuery

Is it a good practice at all? I've never had a problem with this yet.

+12  A: 

In HTML 5 you can do this with the data-* attribute:

<a id="myDiv" data-stackoverflowId="123">...</a>

Since you are using jQuery, you may also want to check the jQuery Metadata Plugin. It will allow you to store and parse JSON metadata in the class attribute, and your markup will still validate.

You would be able to insert your metadata like this:

<a class="your_class {stackoverflowId: '123'}">...</a>

And extract it as follows:

var data = $selectofA.metadata();
Daniel Vassallo
As a matter of fact,I don't care too much about HTML5,but the compatibility with browsers and overhead(so it's better to use fewer plugins).
If you want to be compatible with the current browsers, what you are doing will work in all popular browsers... However using valid markup may help you staying compatible with current **and future** browsers.
Daniel Vassallo
Seriously, isn't the metadata approach adding insult to injury? now we have an attribute value for class that may be CDATA in accordance with the HTML and XHTML spec, but this is very far from the classname or list of classnames that most browser will expect. I wouldn't bet my cookies this does not interfere with the normal behaviour of class attributes.
Roland Bouman
If you want that sort of easy access, please use the Dataset plugin instead, so you can still use `data-` attributes and not clutter up the classnames. We now have a valid way to do this in the spec - let's not use class, kthx. http://orangesoda.net/jquery.dataset.html
Matchu
+7  A: 

If you use data- attributes, it will at least be valid HTML5. See John Resig's blog post on the subject.

Matchu
+1 This is an admission by the HTML5 group that people do this anyway, so we might as well standardize it. It's better than massively overloading the class attribute as people do currently.
Gabe Moothart
see also: http://ajaxian.com/archives/custom-dom-attributes-vs-class-css-styles
dreftymac
+1  A: 

I do this all the time, it works great. Never encountered any problem with this technique whatsoever. I suppose its not a bad idea though to use the data- prefix like suggested in some of the answers.

If you're really paranoid about standards compliance, you could write the HTML in a standard compliant manner and set the attributes on the DOM in a script. At least, the document will validate. But since the net result will be the same, you'd have to be pretty sick to favor that approach to the IMO natural way of extending html as it suits you.

Another approach is to write a custom DTD that defines exactly what extensions you want to use. This approach is described in detail here: http://www.alistapart.com/articles/customdtd/

Basically this approach relies on placing a modified copy of the XHTML DTD at some place, and using a doctype that points to that customized DTD. You would define the extra attribute declarations in that modified DTD, and apparently, the w3c validator will actually use that DTD to validate.

However, I would not use this approach for normal internet facing websites, ever. I'm afraid some browsers might go as far as trying to validate the doc against my dtd, but then give up with an error somewhere halfway. This custom DTD technique is not very widespread I simply wouldn't take the chance for regular webpages.

Roland Bouman
A: 

If you are using jQuery (which was your example) you should look at the data method:

http://api.jquery.com/jQuery.data/

apathetic