diff --git a/entries/data.xml b/entries/data.xml index 560f49d5..cc7a589a 100644 --- a/entries/data.xml +++ b/entries/data.xml @@ -100,10 +100,11 @@ $( "div" ).data( "lastValue" ) === 43; $( "div" ).data( "hidden" ) === true; $( "div" ).data( "options" ).name === "John"; +
The second statement of the code above correctly refers to the data-last-value
attribute of the element. In case there are no data stored with the passed key, jQuery searches among the attributes of the element converting a camel case string into a dashed string, and then prepending data-
to the result. So, the string lastValue
is converted in data-last-value
.
Every attempt is made to convert the string to a JavaScript value (this includes booleans, numbers, objects, arrays, and null). A value is only converted to a number if doing so doesn't change the value's representation. For example, "1E02" and "100.000" are equivalent as numbers (numeric value 100) but converting them would alter their representation so they are left as strings. The string value "100" is converted to the number 100.
-When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON
is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.
To retrieve the value's attribute as a string without any attempt to convert it, use the attr()
method.
The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).
+When the data attribute is an object (starts with '{') or array (starts with '[') then jQuery.parseJSON
is used to parse the string; it must follow valid JSON syntax including quoted property names. If the value isn't parseable as a JavaScript value, it is left as a string.
To retrieve the value's attribute as a string without any attempt to convert it, use the attr()
method.
The data- attributes are pulled in the first time the data property is accessed and then are no longer accessed or mutated (all data values are then stored internally in jQuery).
Calling .data()
with no parameters retrieves all of the values as a JavaScript object. This object can be safely cached in a variable as long as a new object is not set with .data(obj)
. Using the object directly to get or set values is faster than making individual calls to .data()
to get or set each value:
var mydata = $( "#mydiv" ).data();