Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
43 lines (35 sloc) 840 Bytes
<!doctype html>
<html lang="en">
<head>
<title>JavaScript Patterns</title>
<meta charset="utf-8">
</head>
<body>
<script>
/* Title: requery
* Description: avoid requery by using jQuery chaining
*/
// antipattern
// create and append your element
$(document.body).append("<div class='baaron' />");
// requery to bind stuff
$("div.baaron").click(function () {
});
// preferred 1
// swap to appendTo to hold your element
$("<div class='baaron' />")
.appendTo(document.body)
.click(function () {
// do stuff
});
// preferred 2
// cache the selector
var $baaron = $("<div class='baaron' />").appendTo(document.body);
$baaron.click(function () {
// do stuff
});
// References
// http://paulirish.com/2009/perf/
</script>
</body>
</html>