What is the difference in usage between $.data
and $.attr
when using data-someAttribute
?
My understanding is that $.data
is stored within jQuery's $.cache
, not the DOM. Therefore, if I want to use $.cache
for data storage, I should use $.data
. If I want to add HTML5 data-attributes, I should use $.attr("data-attribute", "myCoolValue")
.
If you are passing data to a DOM element from the server, you should set the data on the element:
The data can then be accessed using
.data()
in jQuery:However when you store data on a DOM node in jQuery using data, the variables are stored on the node object. This is to accommodate complex objects and references as storing the data on the node element as an attribute will only accommodate string values.
Continuing my example from above:Also, the naming convention for data attributes has a bit of a hidden "gotcha":
HTML: JS:The hyphenated key will still work:
HTML: JS:However the object returned by
.data()
will not have the hyphenated key set:It's for this reason I suggest avoiding the hyphenated key in javascript.
For HTML, keep using the hyphenated form. HTML attributes are supposed to get ASCII-lowercased automatically, so
<div data-foobar></div>
,<DIV DATA-FOOBAR></DIV>
, and<dIv DaTa-FoObAr></DiV>
are supposed to be treated as identical, but for the best compatibility the lower case form should be preferred.The
HTML: JS:.data()
method will also perform some basic auto-casting if the value matches a recognized pattern:This auto-casting ability is very convenient for instantiating widgets & plugins:
If you absolutely must have the original value as a string, then you'll need to use
HTML: JS:.attr()
:This was a contrived example. For storing color values, I used to use numeric hex notation (i.e. 0xABC123), but it's worth noting that hex was parsed incorrectly in jQuery versions before 1.7.2, and is no longer parsed into a
Number
as of jQuery 1.8 rc 1.jQuery 1.8 rc 1 changed the behavior of auto-casting. Before, any format that was a valid representation of a
HTML: JS:Number
would be cast toNumber
. Now, values that are numeric are only auto-cast if their representation stays the same. This is best illustrated with an example.If you plan on using alternative numeric syntaxes to access numeric values, be sure to cast the value to a
JS (cont.):Number
first, such as with a unary+
operator.The main difference between the two is where it is stored and how it is accessed.
$.fn.attr
stores the information directly on the element in attributes which are publicly visible upon inspection, and also which are available from the element's native API.$.fn.data
stores the information in a ridiculously obscure place. It is located in a closed over local variable calleddata_user
which is an instance of a locally defined function Data. This variable is not accessible from outside of jQuery directly.Data set with
attr()
$(element).attr('data-name')
element.getAttribute('data-name')
,data-name
also accessible from$(element).data(name)
andelement.dataset['name']
andelement.dataset.name
Data set with
.data()
.data(name)
.attr()
or anywhere else