1

I'm looking at using the jQuery MetaData Plugin. Looks very interesting but...

<li class="someclass {some: 'data'} anotherclass">...</li>

<script>alert($('li.someclass').metadata().some);</script>

does this code validate?

update

Of course this is an old example (2010), nowadays we use $.data() and element.dataset, for setting data attributes in html5/jquery. Quite useful too.

qodeninja
  • 10,946
  • 30
  • 98
  • 152

2 Answers2

5

Well according to the XHTML Strict DTD (and, I think, all the other relevant DTDs), the "class" attribute is CDATA, so that means just about anything goes in the value.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

If you are using jQuery, just use the .data() method!

$(elem).data(key, value);
//e.g. store a string
$('#someID').data('secret','my voice is my password, verify me!');
//or store some JSON
$('#otherID').data('stuff', JSONData);
scunliffe
  • 62,582
  • 25
  • 126
  • 161
  • 3
    Im tired of hearing this suggestion! – qodeninja Mar 10 '10 at 00:17
  • 2
    @codeninja - sorry about that. I was under the impression that this was the desired outcome. Storing JSON data on an element that doesn't wreck your valid (X)HTML. Using the class attribute can work, but it seems like a kludgy abuse of the attribute IMHO. – scunliffe Mar 10 '10 at 00:39
  • 1
    @codeninja - ah, it wasn't clear that you were looking for a solution to store the data *only* in the HTML to be pulled out later. (I found your other question on the same topic). class should work then unless you want to use the HTML5 data- attributes (knowing they will break XHTML strict in older browsers). – scunliffe Mar 10 '10 at 01:02