FND / jquery

jQuery plugins

This URL has Read+Write access

jquery / idioms.html
100644 146 lines (127 sloc) 3.796 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jQuery idioms</title>
<style type="text/css">
h1,
h2,
h3,
h4 {
margin: 0.5em 0 0.2em;
}
 
pre {
margin: 10px;
padding: 5px;
color: #080;
background-color: #F8F8F8;
}
 
.sample {
margin: 10px 10px 10px 5em;
border: 1px solid #BABA45;
padding: 5px;
background-color: #F5F5B5;
}
</style>
</head>
 
<body>
<h1>jQuery idioms</h1>
 
<ul>
<li>
<a href="#generating_multiple">dynamically generating multiple elements</a>
</li>
<li>
<a href="#generating_nested">dynamically generating nested element structures</a>
</li>
</ul>
 
(<a href="http://fnd.lewcid.org/blog/archive/109">blog post</a> explaining background)
 
<h2 id="generating_multiple">dynamically generating multiple elements</h2>
 
<h3>before</h3>
<pre>
var entries = ["foo", "bar", "baz"];
 
var list = $("&lt;ul /&gt;").appendTo(wrapper);
$.each(entries, function(i, item) {
$("&lt;li /&gt;").text(item).appendTo(list);
});
</pre>
 
<h3>after</h3>
<pre>
var entries = ["foo", "bar", "baz"];
 
$("&lt;ul /&gt;").
append($.map(entries, function(item, i) {
return $("&lt;li /&gt;").text(item)[0]
})).
appendTo(wrapper);
</pre>
<p>
Note that <code>map</code> returns DOM elements, not jQuery objects.
</p>
 
<h2 id="generating_nested">dynamically generating nested element structures</h2>
 
<h3>before</h3>
<pre>
var container = $("&lt;div /&gt;").appendTo(wrapper);
$("&lt;h4 /&gt;").addClass("subheading").text("dynamic elements").appendTo(container);
var list = $("&lt;dl /&gt;").fadeIn("slow").appendTo(container);
$("&lt;dt /&gt;").text("foo").appendTo(list);
$("&lt;dd /&gt;").text("lorem ipsum").appendTo(list);
$("&lt;dt /&gt;").text("bar").appendTo(list);
$("&lt;dd /&gt;").text("dolor sit amet").appendTo(list);
</pre>
 
<h3>after</h3>
<pre>
$("&lt;div /&gt;").
append("&lt;h4 /&gt;").children(":last").
addClass("subheading").text("dynamic elements").
end().
append("&lt;dl /&gt;").children(":last").
append("&lt;dt /&gt;").children(":last").text("foo").end().
append("&lt;dd /&gt;").children(":last").text("lorem ipsum").end().
append("&lt;dt /&gt;").children(":last").text("bar").end().
append("&lt;dd /&gt;").children(":last").text("dolor sit amet").end().
fadeIn("slow").
end().
appendTo(wrapper);
</pre>
<p>
When appending an element, it is selected and modified, then the selection returns to the parent.
</p>
<p>
The key is that <code>append</code> supports HTML as argument for creating new objects, which
allows for elaborate chaining.
(This might seem obvious in hindsight... )
</p>
<p>
A simple utility method could be used to increase expressiveness:
</p>
<pre>
$.fn.attach = function(html) {
return this.append(html).children(":last");
};
 
$("&lt;div /&gt;").
attach("&lt;h4 /&gt;").
addClass("subheading").text("dynamic elements").
end().
attach("&lt;dl /&gt;").
attach("&lt;dt /&gt;").text("foo").end().
attach("&lt;dd /&gt;").text("lorem ipsum").end().
attach("&lt;dt /&gt;").text("bar").end().
attach("&lt;dd /&gt;").text("dolor sit amet").end().
fadeIn("slow").
end().
appendTo(wrapper);
</pre>
 
<!-- dynamic demos -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function() {
 
var evaluateContents = function(ev) {
var code = $(this).prev().text();
var wrapper = $('<div class="sample" />').insertAfter(this);
eval(code);
};
$("pre").after('<input type="button" value="test" title="evaluate code" />').next().
click(evaluateContents);
});
</script>
</body>
 
</html>