Home > Javascript, Web Design > Storing Data using jQuery

Storing Data using jQuery

A lot of developers tend to store additional data in HTML attributes which are not meant to store data. In order to solve this issue jQuery offers a data method with which you can store any data you might need.

Overview of the data method

Stores the value in the named spot.
If the jQuery collection references multiple elements, the data element is set on all of them. A data value that is a Javascript object is not copied and will be shared across all elements in the collection.

This function can be useful for attaching data to elements without having to create a new expando. It also isn’t limited to a string. The value can be any format.

Example:

$('selector').data('param', 'store whatever data you need');
// then later getting the data with
$('selector').data('param'); // Get data stored at this param
Categories: Javascript, Web Design Tags: