<p>I love how I can use jQuery to solve all of my problems. When I was working on a website for <a href="http://www.thenationaltree.org/">The Lighting of the National Christmas Tree</a>, I had to create a page that displayed all of the ornaments. Thank god for the jQuery <a href="http://docs.jquery.com/Manipulation/clone">clone()</a> method, which saved me loads of time and effort.</p>

<h2>The Situation</h2>
<p>I was on a tight deadline, and had to figure out some way to layout all of the state ornaments. Not to mention the fact that I had to crop and resize 56 images to 2 different sizes. Yes, I know there are only 50 states, but the US territories were included as well.</p>
<p>So the layout had to be easy enough for everyone to see all the ornaments and be able to click to get a more detailed photo and some information about the ornament. I really was not interested in creating 56 separate pages with information about each ornament, so I went scouring through the jQuery documentation to see how I could copy elements.</p>
<p>Before I get started describing what I had to do, <a href="//trevor-davis.com/play/using-jquery-clone-for-a-lightbox/">here is a demo page that I created</a>.</p>
<h2>My Solution</h2>
<p>So basically, this is what I wanted to happen when an ornament was clicked:</p>
<ol><li>Get the source of the larger image to be shown</li>
<li>Copy the description text about the ornament</li>
<li>Append the image and text and open it in a modal window</li>
</ol><p>The styling of the example is nothing special, so I will stick to explaining the script.</p>
<h3>The Script</h3>
<p>First, we want all of this to happen after the document is loaded and when the ornament links are clicked:</p>
<div class="codeblock"><code>
$(document).ready(function() {<br /> $('ul.ornaments a').click(function() {<br />  return false;<br /> });<br />}); 

</code></div>
<p>Next, we want to setup the modal window with a couple of empty divs: <em>(Note: line breaks added for readability)</em></p>
<div class="codeblock"><code>
$('body').append('&lt;div id="overlay"&gt;&lt;/div&gt;<br /> &lt;div id="modal"&gt;<br />  &lt;div id="modalInner"&gt;<br />   &lt;a href="#" id="close" title="Close"&gt;Close&lt;/a&gt;<br />   &lt;div id="modalImageColumn"&gt;&lt;/div&gt;<br />   &lt;div id="modalTextColumn"&gt;&lt;/div&gt;<br />   &lt;div class="clearing"&gt;&lt;/div&gt;<br />  &lt;/div&gt;<br /> &lt;/div&gt;'); 

</code></div>
<p>Since IE6 doesn’t support fixed positioning, we need to get the actual height of the page so we can apply that height to the overlay:</p>
<div class="codeblock"><code>
if($.browser.msie &amp;&amp; $.browser.version &lt;= 6.0) {<br /> var windowHeight = $(document).height();<br /> $('#overlay').height(windowHeight);<br />} 

</code></div>
<p>Next, we set the opacity of the overlay, start to fade it in, position the modal window, and start to fade it in.</p>
<div class="codeblock"><code>
$('#overlay').css('opacity', 0.5).fadeIn('fast');<br />$('#modal').fadeIn('slow');  <br />var scrollPos = $(window).scrollTop() + 50;  <br />$('div#modal').css('top',scrollPos+'px'); 

</code></div>
<p>The order of these things can be moved around some, but this is what I ended up doing, and everything looked good.</p>
<p>Next, we want to close and remove the modal window when the close button is clicked or the background overlay is clicked:</p>
<div class="codeblock"><code>
$('div#modal a#close,div#overlay').bind('click', function() {<br /> $('div#modal').fadeOut('fast',function() {<br />  $('div#overlay').fadeOut('fast',function() {<br />   $(this).remove();          <br />  });<br />  $(this).remove();          <br /> });<br /> return false;<br />}); 

</code></div>
<p>Next, we grab the url of the link that was clicked, which goes to the full size image, and append the image to the modal window. Once the image has been loaded, we fade it in:</p>
<div class="codeblock"><code>
var imgUrl = $(this).attr('href');<br />$('div#modalImageColumn').append('&lt;img src="'+imgUrl+'" alt="" class="frame" height="400" id="ornament" width="400" /&gt;');<br />$('img#ornament').load(function() {<br /> $(this).fadeIn();        <br />}); 

</code></div>
<p>Finally, we get to use the clone() method. We target the div with the class of <strong>stateDescription</strong> that is a sibling of the link we clicked, clone it, and append it to the modal window:</p>
<div class="codeblock"><code>
$(this).siblings('.stateDescription').clone().appendTo('div#modalTextColumn'); 

</code></div>
<h2>Conclusion</h2>
<p>Pretty cool stuff if I say so myself. I don’t think there are any jQuery plugins out there that would have helped me achieve this, which is why it is important to actually understand jQuery.</p>

<p><a href="//trevor-davis.com/play/using-jquery-clone-for-a-lightbox/" class="more-link">See Demo</a></p>

<p><a href="//trevor-davis.com/images/content/2008/12/modal.js">Here is the full script to download</a>. You can also take a look at the <a href="http://www.thenationaltree.org/visit-santa/pathway-of-peace/">final product on the National Christmas Tree site</a>.</p>

<p>But, I would encourage you to actually understand jQuery instead of just grabbing code and using it.</p>