Skip to content

Commit 47b36c9

Browse files
Mottiekswedberg
authored andcommitted
Add .data(obj) example
1 parent c5a906f commit 47b36c9

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

entries/data.xml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
<p>The <code>.data()</code> method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.</p>
2424
<p> We can set several distinct values for a single element and retrieve them later:</p>
2525
<pre><code>
26-
$('body').data('foo', 52);
27-
$('body').data('bar', { myType: 'test', count: 40 });
26+
$("body").data("foo", 52);
27+
$("body").data("bar", { myType: "test", count: 40 });
28+
$("body").data({ baz: [ 1, 2, 3 ] });
2829

29-
$('body').data('foo'); // 52
30-
$('body').data(); // {foo: 52, bar: { myType: 'test', count: 40 }}
30+
$("body").data("foo"); // 52
31+
$("body").data(); // { foo: 52, bar: { myType: "test", count: 40 }, baz: [ 1, 2, 3 ] }
3132
</code></pre>
3233
<p>In jQuery 1.4.3 setting an element's data object with <code>.data(obj)</code> extends the data previously stored with that element. jQuery itself uses the <code>.data()</code> method to save information under the names 'events' and 'handle', and also reserves any data name starting with an underscore ('_') for internal use.</p>
3334
<p>Prior to jQuery 1.4.3 (starting in jQuery 1.4) the .data() method completely replaced all data, instead of just extending the data object. If you are using third-party plugins it may not be advisable to completely replace the element's data object, since plugins may have also set data.</p>

0 commit comments

Comments
 (0)