4 ms·
The jQuery metadata plugin makes elegant use of this property of class names. For example, you can do something like this: <a href="#hello" class="{name:'Geor
by qeorge 16y ago
The jQuery metadata plugin makes elegant use of this property of class names.
For example, you can do something like this:
<a href="#hello" class="{name:'George'}" />
and then in jQuery you can retrieve the metadata like so:
$('a').metadata('name'); // returns "George"
Its incredibly useful for associating metadata with DOM elements without breaking the HTML's validity.
- bentruyman 16y agoIsn't this what the data-* attributes should be used for though? Storing that kind of stuff in the `class` attribute seems semantically flawed. http://html5doctor.com/html5-custom-data-attributes/ http://html5doctor.com/html5-custom-data-attributes/
- eli 16y agoIck. I think I'd rather just break my HTML and add random attributes.
- qeorge 16y agoWhy is that better? You can use JSON here. (I say this as someone who used to use random HTML attributes.)
- eli 16y agoI find it aesthetically displeasing, and I'm not convinced that abusing the class attribute to store metadata is "more correct" than just adding your own attributes which.
- edd 16y agoHTML5 has a proposal for a proper way to do this: http://dev.w3.org/html5/spec/elements.html#embedding-custom-non-visible-data http://dev.w3.org/html5/spec/elements.html#embedding-custom-...
- qeorge 16y agoThis works now, in all relevant browsers, which is what I need.