|
33 | 33 | </div>
|
34 | 34 | </fieldset>
|
35 | 35 | </form>
|
36 |
| -</code></pre> |
| 36 | + </code></pre> |
37 | 37 | <p>To get a comma-separated list of checkbox <code>ID</code>s:</p>
|
38 |
| - <pre><code>$(':checkbox').map(function() { |
39 |
| - return this.id; |
40 |
| - }).get().join();</code></pre> |
| 38 | + <pre><code> |
| 39 | +$( ":checkbox" ) |
| 40 | + .map(function() { |
| 41 | + return this.id; |
| 42 | + }) |
| 43 | + .get() |
| 44 | + .join(); |
| 45 | + </code></pre> |
41 | 46 | <p>The result of this call is the string, <code>"two,four,six,eight"</code>.</p>
|
42 | 47 | <p>Within the callback function, <code>this</code> refers to the current DOM element for each iteration. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns <code>null</code> or <code>undefined</code>, no element will be inserted.</p>
|
43 | 48 | </longdesc>
|
44 | 49 | <example>
|
45 | 50 | <desc>Build a list of all the values within a form.</desc>
|
46 | 51 | <code><![CDATA[
|
47 |
| - $("p").append( $("input").map(function(){ |
48 |
| - return $(this).val(); |
49 |
| - }).get().join(", ") ); |
| 52 | +$( "p" ) |
| 53 | + .append( $( "input" ).map(function() { |
| 54 | + return $( this ).val(); |
| 55 | + }) |
| 56 | + .get() |
| 57 | + .join( ", " ) ); |
50 | 58 | ]]></code>
|
51 | 59 | <css><![CDATA[
|
52 |
| - p { color:red; } |
53 |
| - ]]></css> |
54 |
| - <html><![CDATA[<p><b>Values: </b></p> |
55 |
| - <form> |
56 |
| - <input type="text" name="name" value="John"/> |
57 |
| -
|
58 |
| - <input type="text" name="password" value="password"/> |
59 |
| - <input type="text" name="url" value="http://ejohn.org/"/> |
60 |
| -
|
61 |
| - </form>]]></html> |
| 60 | + p { |
| 61 | + color: red; |
| 62 | + } |
| 63 | +]]></css> |
| 64 | + <html><![CDATA[ |
| 65 | +<p><b>Values: </b></p> |
| 66 | +<form> |
| 67 | + <input type="text" name="name" value="John"> |
| 68 | + <input type="text" name="password" value="password"> |
| 69 | + <input type="text" name="url" value="http://ejohn.org/"> |
| 70 | +</form> |
| 71 | +]]></html> |
62 | 72 | </example>
|
63 | 73 | <example>
|
64 | 74 | <desc>A contrived example to show some functionality.</desc>
|
65 | 75 | <code><![CDATA[
|
66 |
| -var mappedItems = $("li").map(function (index) { |
67 |
| - var replacement = $("<li>").text($(this).text()).get(0); |
| 76 | +var mappedItems = $( "li" ).map(function( index ) { |
| 77 | + var replacement = $( "<li>" ).text( $( this ).text() ).get( 0 ); |
68 | 78 | if (index == 0) {
|
69 |
| - /* make the first item all caps */ |
70 |
| - $(replacement).text($(replacement).text().toUpperCase()); |
71 |
| - } else if (index == 1 || index == 3) { |
72 |
| - /* delete the second and fourth items */ |
| 79 | + // Make the first item all caps |
| 80 | + $( replacement ).text( $( replacement ).text().toUpperCase() ); |
| 81 | + } else if ( index == 1 || index == 3 ) { |
| 82 | + // Delete the second and fourth items |
73 | 83 | replacement = null;
|
74 |
| - } else if (index == 2) { |
75 |
| - /* make two of the third item and add some text */ |
76 |
| - replacement = [replacement,$("<li>").get(0)]; |
77 |
| - $(replacement[0]).append("<b> - A</b>"); |
78 |
| - $(replacement[1]).append("Extra <b> - B</b>"); |
| 84 | + } else if ( index == 2 ) { |
| 85 | + // Make two of the third item and add some text |
| 86 | + replacement = [ replacement, $( "<li>" ).get( 0 ) ]; |
| 87 | + $( replacement[ 0 ] ).append( "<b> - A</b>" ); |
| 88 | + $( replacement[ 1 ] ).append( "Extra <b> - B</b>" ); |
79 | 89 | }
|
80 | 90 |
|
81 |
| - /* replacement will be a dom element, null, |
82 |
| - or an array of dom elements */ |
| 91 | + // Replacement will be a dom element, null, |
| 92 | + // or an array of dom elements |
83 | 93 | return replacement;
|
84 | 94 | });
|
85 |
| -$("#results").append(mappedItems); |
86 |
| -
|
| 95 | +$( "#results" ).append( mappedItems ); |
87 | 96 | ]]></code>
|
88 | 97 | <css><![CDATA[
|
89 |
| - body { font-size:16px; } |
90 |
| - ul { float:left; margin:0 30px; color:blue; } |
91 |
| - #results { color:red; } |
92 |
| - ]]></css> |
93 |
| - <html><![CDATA[<ul> |
94 |
| - <li>First</li> |
95 |
| - <li>Second</li> |
96 |
| - <li>Third</li> |
97 |
| -
|
98 |
| - <li>Fourth</li> |
99 |
| - <li>Fifth</li> |
100 |
| - </ul> |
101 |
| - <ul id="results"> |
102 |
| -
|
103 |
| - </ul>]]></html> |
| 98 | + body { |
| 99 | + font-size: 16px; |
| 100 | + } |
| 101 | + ul { |
| 102 | + float: left; |
| 103 | + margin: 0 30px; |
| 104 | + color: blue; |
| 105 | + } |
| 106 | + #results { |
| 107 | + color: red; |
| 108 | + } |
| 109 | +]]></css> |
| 110 | + <html><![CDATA[ |
| 111 | +<ul> |
| 112 | + <li>First</li> |
| 113 | + <li>Second</li> |
| 114 | + <li>Third</li> |
| 115 | + <li>Fourth</li> |
| 116 | + <li>Fifth</li> |
| 117 | +</ul> |
| 118 | +<ul id="results"> |
| 119 | +</ul> |
| 120 | +]]></html> |
104 | 121 | </example>
|
105 | 122 | <example>
|
106 | 123 | <desc>Equalize the heights of the divs.</desc>
|
107 | 124 | <code><![CDATA[
|
108 | 125 | $.fn.equalizeHeights = function() {
|
109 |
| - var maxHeight = this.map(function(i,e) { |
110 |
| - return $(e).height(); |
| 126 | + var maxHeight = this.map(function( i, e ) { |
| 127 | + return $( e ).height(); |
111 | 128 | }).get();
|
112 |
| -
|
113 | 129 | return this.height( Math.max.apply(this, maxHeight) );
|
114 | 130 | };
|
115 | 131 |
|
116 |
| -$('input').click(function(){ |
117 |
| - $('div').equalizeHeights(); |
| 132 | +$( "input" ).click(function() { |
| 133 | + $( "div" ).equalizeHeights(); |
118 | 134 | });
|
119 |
| -
|
120 | 135 | ]]></code>
|
121 | 136 | <css><![CDATA[
|
122 |
| -div { width: 40px; float:left; } |
123 |
| -input { clear:left} |
124 |
| - ]]></css> |
| 137 | + div { |
| 138 | + width: 40px; |
| 139 | + float: left; |
| 140 | + } |
| 141 | + input { |
| 142 | + clear: left; |
| 143 | + } |
| 144 | +]]></css> |
125 | 145 | <html><![CDATA[
|
126 |
| -
|
127 | 146 | <input type="button" value="equalize div heights">
|
128 |
| -
|
129 | 147 | <div style="background:red; height: 40px; "></div>
|
130 | 148 | <div style="background:green; height: 70px;"></div>
|
131 | 149 | <div style="background:blue; height: 50px; "></div>
|
132 |
| -
|
133 | 150 | ]]></html>
|
134 | 151 | </example>
|
135 | 152 | <category slug="traversing/filtering"/>
|
|
0 commit comments