<!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 = $("<ul />").appendTo(wrapper);
$.each(entries, function(i, item) {
$("<li />").text(item).appendTo(list);
});
</pre>
<h3>after</h3>
<pre>
var entries = ["foo", "bar", "baz"];
$("<ul />").
append($.map(entries, function(item, i) {
return $("<li />").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 = $("<div />").appendTo(wrapper);
$("<h4 />").addClass("subheading").text("dynamic elements").appendTo(container);
var list = $("<dl />").fadeIn("slow").appendTo(container);
$("<dt />").text("foo").appendTo(list);
$("<dd />").text("lorem ipsum").appendTo(list);
$("<dt />").text("bar").appendTo(list);
$("<dd />").text("dolor sit amet").appendTo(list);
</pre>
<h3>after</h3>
<pre>
$("<div />").
append("<h4 />").children(":last").
addClass("subheading").text("dynamic elements").
end().
append("<dl />").children(":last").
append("<dt />").children(":last").text("foo").end().
append("<dd />").children(":last").text("lorem ipsum").end().
append("<dt />").children(":last").text("bar").end().
append("<dd />").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");
};
$("<div />").
attach("<h4 />").
addClass("subheading").text("dynamic elements").
end().
attach("<dl />").
attach("<dt />").text("foo").end().
attach("<dd />").text("lorem ipsum").end().
attach("<dt />").text("bar").end().
attach("<dd />").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>