Documentation

Plugins/Metadata/metadata

From jQuery JavaScript Library

Jump to: navigation, search

« 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:

NameType
typeStringDefault: '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'
})

nameStringDefault: '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'
})

singleStringDefault: '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:

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>

NameType