From jQuery JavaScript Library
« Back to Plugins/Metadata
metadata( [options] )
Extracts, caches, and returns metadata from the first element in the jQuery collection.
Arguments:| options (Optional) | Options | |
|---|
| A set of key/value pairs that define the type of metadata to be extracted. All options are optional. |
Options:| Name | Type |
| type | String | Default: 'class' |
|---|
| Specify the expected locations of metadata for the element. Possible values are 'class': search in the class attribute, 'elem': search for an element inside the element being searched, and 'attr': search in a custom attribute on the element. |
Searches for metadata in a custom element attribute instead of in the class.
$(".selector").metadata({
type: 'attr'
})
|
| name | String | Default: 'metadata' |
|---|
| When type is 'attr', specify the name of the custom attribute for which to search. When type is 'elem', specify the tag name of the element for which to search. |
Searches for metadata in a custom element attribute with a name of 'jdata'.
$(".selector").metadata({
type: 'attr',
name: 'jdata'
})
|
| single | String | Default: 'metadata' |
|---|
| The name given to the data extracted from the element in the jQuery cache. |
Stores and retrieves the data extracted into an item named 'jdata' in the jQuery cache.
$(".selector").metadata({
single: 'jdata'
})
|
Examples:| Name | Type |
Gets metadata from the class attribute.
<li class="someclass {some: 'data'} anotherclass">...</li>
<script>alert($('li.someclass').metadata().some);</script>
Gets metadata from a custom attribute.
<li data="{some:'random', json: 'data'}">...</li>
<script>alert($('li.someclass').metadata({type:'attr',name:'data'}).some);</script>
Gets metadata from a child element.
<li class="someclass"><script type="application/json">{some:"json",data:true}</script>...</li>
<script>alert($('li.someclass').metadata({type:'elem',name:'script'}).some);</script>