Back

.data( [key], [value] )

Store or read arbitrary data associated with the matched elements

If key and value are provided, this will set data for each element and return the current set for chaining

If only key {Object} is provided, this will set data for each element (as key/value pairs) and return the current set for chaining

If only key {String} is provided, this will return the data stored under the given key for the first element in the current set

If no arguments are passed, this will return all of the data stored for the first element in the current set

Note: setting data through dollar does NOT create corresponding data-attributes on the element

Params

key{Object/String} [optional]An Object of key/value pairs to store, or the String key from which to store/read a value
value{Any} [optional]A value to be set. Most values will be converted to a String before setting. Functions will be evaluted with (previousValue, index) and the return value will be set.

Returns

Data or DollarJS (chainable)

Examples

$('p').data('foo', 'bar')
$('p').data({ foo: 'bar' })
$('p').data('foo', function(previousValue, index){ return 'foo'; })
$('p').data('foo')
$('p').data()