Skip to content
This repository
Newer
Older
100644 42 lines (28 sloc) 1.621 kb
d16f72f9 » danheberden
2011-03-23 Add rest of using-jquery-core section
1 ---
2 title : Data Methods
b61492f0 » ajpiano
2012-03-22 unify "jQuery Basics" and "Using jQuery Core" into a single chapter. …
3 level: intermediate
32e02dd4 » RedWolves
2012-10-16 Added attribution for jQuery Fundamentals, closes #151
4 source: http://jqfundamentals.com/legacy
b66c273d » alimony
2013-03-14 Remove remaining trailing whitespace from all pages. Fixes #313.
5 attribution:
32e02dd4 » RedWolves
2012-10-16 Added attribution for jQuery Fundamentals, closes #151
6 - jQuery Fundamentals
d16f72f9 » danheberden
2011-03-23 Add rest of using-jquery-core section
7 ---
810becfd » alimony
2013-05-17 Style/typography fixes and code style adherence in the jQuery Core se…
8
9d45ae23 » jorydotcom
2012-11-15 Style Guide conformance for Using jQuery Core sections. Fixes #188.
9 There's often data about an element you want to store with the element. In plain JavaScript, you might do this by adding a property to the DOM element, but you'd have to deal with memory leaks in some browsers. jQuery offers a straightforward way to store data related to an element, and it manages the memory issues for you.
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
10
81cecfee » ajpiano
2012-10-15 Code formatting changes for Using jQuery Core chapter
11 ```
810becfd » alimony
2013-05-17 Style/typography fixes and code style adherence in the jQuery Core se…
12 // Storing and retrieving data related to an element.
13
14 $( "#myDiv" ).data( "keyName", { foo: "bar" } );
4a71fa80 » gnarf
2012-11-22 Example Code Style Guidance - Comments before lines, quotes, etc. - F…
15
810becfd » alimony
2013-05-17 Style/typography fixes and code style adherence in the jQuery Core se…
16 $( "#myDiv" ).data( "keyName" ); // Returns { foo: "bar" }
81cecfee » ajpiano
2012-10-15 Code formatting changes for Using jQuery Core chapter
17 ```
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
18
6179e0c0 » rmurphey
2013-05-17 First pass at fixing up references to $.fn methods. See #291.
19 Any kind of data can be stored on an element. For the purposes of this article, `.data()` will be used to store references to other elements.
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
20
6179e0c0 » rmurphey
2013-05-17 First pass at fixing up references to $.fn methods. See #291.
21 For example, you may want to establish a relationship between a list item and a `<div>` that's inside of it. This relationship could be established every single time the list item is touched, but a better solution would be to establish the relationship once, then store a pointer to the `<div>` on the list item using `.data()`:
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
22
81cecfee » ajpiano
2012-10-15 Code formatting changes for Using jQuery Core chapter
23 ```
6179e0c0 » rmurphey
2013-05-17 First pass at fixing up references to $.fn methods. See #291.
24 // Storing a relationship between elements using .data()
d244c320 » bobholt
2012-10-21 apply core styles to using-jquery-core sectione examples
25
810becfd » alimony
2013-05-17 Style/typography fixes and code style adherence in the jQuery Core se…
26 $( "#myList li" ).each(function() {
27
d6178956 » scottgonzalez
2014-04-07 All: Don't use Hungarian notation for jQuery objects
28 var li = $( this );
29 var div = li.find( "div.content" );
d244c320 » bobholt
2012-10-21 apply core styles to using-jquery-core sectione examples
30
d6178956 » scottgonzalez
2014-04-07 All: Don't use Hungarian notation for jQuery objects
31 li.data( "contentDiv", div );
d244c320 » bobholt
2012-10-21 apply core styles to using-jquery-core sectione examples
32
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
33 });
34
810becfd » alimony
2013-05-17 Style/typography fixes and code style adherence in the jQuery Core se…
35 // Later, we don't have to find the div again;
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
36 // we can just read it from the list item's data
d6178956 » scottgonzalez
2014-04-07 All: Don't use Hungarian notation for jQuery objects
37 var firstLi = $( "#myList li:first" );
d244c320 » bobholt
2012-10-21 apply core styles to using-jquery-core sectione examples
38
d6178956 » scottgonzalez
2014-04-07 All: Don't use Hungarian notation for jQuery objects
39 firstLi.data( "contentDiv" ).html( "new content" );
81cecfee » ajpiano
2012-10-15 Code formatting changes for Using jQuery Core chapter
40 ```
e6bed33f » ajpiano
2011-11-02 update formatting on using-jquery-core and plugins chapters
41
6179e0c0 » rmurphey
2013-05-17 First pass at fixing up references to $.fn methods. See #291.
42 In addition to passing `.data()` a single key-value pair to store data, you can also pass an object containing one or more pairs.
Something went wrong with that request. Please try again.