Skip to content

Commit a2b3e25

Browse files
committed
added jquery patterns
1 parent 64c810c commit a2b3e25

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

jquery-patterns/append.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
// appending inside
11+
$.each(reallyLongArray, function(count, item) {
12+
var newLI = '<li>' + item + '</li>';
13+
$('#ballers').append(newLI);
14+
});
15+
16+
// documentFragment off-DOM
17+
var frag = document.createDocumentFragment();
18+
$.each(reallyLongArray, function(count, item) {
19+
var newLI = '<li>' + item + '</li>';
20+
frag.appendChild(newLI[0]);
21+
});
22+
$('#ballers')[0].appendChild(frag);
23+
24+
// string concatenate and set innerHTML
25+
var myhtml = '';
26+
$.each(reallyLongArray, function(count, item) {
27+
myhtml += '<li>' + item + '</li>';
28+
});
29+
$('#ballers').html(myhtml);
30+
</script>
31+
</body>
32+
</html>

jquery-patterns/requery.html

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
// create and append your element
11+
$(document.body).append("<div class='baaron' />");
12+
// requery to bind stuff
13+
$("div.baaron").click(function(){});
14+
15+
// preferred
16+
// swap to appendTo to hold your element
17+
$("<div class='baaron' />")
18+
.appendTo(document.body)
19+
.click(function(){});
20+
</script>
21+
</body>
22+
</html>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!doctype html>
2+
<html lang="en">
3+
<head>
4+
<title>JavaScript Patterns</title>
5+
<meta charset="utf-8">
6+
</head>
7+
<body>
8+
<script>
9+
// antipattern
10+
$('.buttons > *')
11+
// preferred
12+
$('.buttons').children()
13+
14+
// antipattern
15+
$('.gender :radio')
16+
$('.gender *:radio')
17+
// preferred
18+
$('.gender input:radio')
19+
</script>
20+
</body>
21+
</html>

0 commit comments

Comments
 (0)