jQuery `.data` can store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.
jQuery's
.data
Store arbitrary data associated with the matched elements or return the value at the named data store for the first element in the set of matched elements.
Example
One short example of use is to set data with `
$("body").data("foo", "bar");
which can later be returned by calling
$("body").data("foo");`,
which will return "bar"
.
You can also use .data
to return the value of a custom data-
attribute. For example, if you have the HTML element
<div id="myDiv" data-info="My Info"></div>`
you can call
$("#myDiv").data('info');
which will return "My Info"
. Note that .data
will not edit or add these attributes, only read.