Edit this page

up.script up.data(element)
JavaScript function

Returns the data attached to the given element.

Returns an empty object if the element has no attached data.

Multiple up.data() calls for the same object always return the same object reference.

Use with [up-data]

You have an element with JSON data serialized into an [up-data] attribute:

<span class='person' up-data='{ "age": 18, "name": "Bob" }'>Bob</span>

Calling up.script.data() will deserialize the JSON string into a JavaScript object:

up.data('.person') // returns { age: 18, name: 'Bob' }

Use with data attributes

You may also use standard [data-*] attributes to attach data to an element.

<span class='person' data-first-name='Alice' data-last-name='Anderson'>Alice</span>

The object returned by up.data() will contain all data attributes with camelCased keys:

up.data('.person') // returns { firstName: 'Alice', lastName: 'Anderson' }

You may use both [up-data] and [data-*] attributes on the same element. The object returned by up.data() will contain values from both.


Parameters

element stringorElementorjQuery

The element for which to return data.

Return value

The data attached to the element.

Returns an empty object if the element has no attached data.

Multiple up.data() calls for the same object always return the same object reference.