<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>
<channel>
	<title>The Canvas of Ryan SadwickTag Archive | nth child | The Canvas of Ryan Sadwick</title>
	<atom:link href="http://ryan.sadwick.com/tag/nth-child/feed/" rel="self" type="application/rss+xml" />
	<link>http://ryan.sadwick.com</link>
	<description>Enter the mind of Ryan Sadwick, professional computer programmer.</description>
	<lastBuildDate>Sun, 21 Oct 2012 15:54:53 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>The Nth Child Selector in jQuery</title>
		<link>http://ryan.sadwick.com/nth-child/</link>
		<comments>http://ryan.sadwick.com/nth-child/#comments</comments>
		<pubDate>Mon, 16 May 2011 20:05:01 +0000</pubDate>
		<dc:creator>Ryan Sadwick</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[nth child]]></category>
		<guid isPermaLink="false">http://ryan.sadwick.com/?p=762</guid>
		<description><![CDATA[The :nth-child() selector in jQuery is fun to use and really helps out when you have a nice design that needs you to take full advantage of it. Consider this layout below: This layout has three items. The middle item has a shadow. While this could be solved by some hard coded CSS&#8230; what if [...]]]></description>
				<content:encoded><![CDATA[<p><!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic -->
<p class="DropCaps"><a href="http://api.jquery.com/nth-child-selector/">The :nth-child() selector in jQuery</a> is fun to use and really helps out when you have a nice design that needs you to take full advantage of it.</p>
<p>Consider this layout below:</p>
<p><span class="PhotoTextLeft"><a rel="prettyPhoto[gallWeb]" href="http://ryan.sadwick.com/wp-content/uploads/2011/05/layout.jpg"><img class="alignnone size-full wp-image-36" title="Layout" src="http://ryan.sadwick.com/wp-content/uploads/2011/05/layout.jpg" alt="Layout" /></a></span></p>
<p>This layout has three items.  The middle item has a shadow.  While this could be solved by some hard coded CSS&#8230; what if this is a dynamic news list (which is what it is!) where the total number of news items change daily.</p>
<p>To solve this, we’ll make use of the Nth Child selector.  But first, let’s consider a simple example so you can better understand what we’re going to do.</p>
<h3>A simple example</h3>
<p class="DropCaps">Let’s say we have two unordered lists.  We need to color the background of the second item in each list:</p>
<p>In jQuery, we can set the nth-child to 2.  Then, let’s chain with some css and change the background color:</p>
<p><script class="brush: js" type="syntaxhighlighter"><![CDATA[
 $(document).ready(function() {
	//Color only the second item in each list:
	$("li:nth-child(2)").css("background-color", "#ffcc99");
	});
</script></p>
<div class="Citation">
<hr />
<p><cite>:nth-child(n)  is "1-indexed".  You may realize that this is unusual since most of jQuery’s selectors are 0 based.  This selector is different since it’s <a href="http://www.w3.org/TR/css3-selectors/#nth-child-pseudo">derived from the CSS specification</a>.<br />
</cite></p>
<hr />
<p><a href="http://jsfiddle.net/rsadwick/9QTF7/">Click here to see the first example via jsFiddle</a></p>
<p><a href="http://ryan.sadwick.com/wp-content/uploads/2011/05/sample1.jpg"><img class="alignnone size-full wp-image-799" title="sample1" src="http://ryan.sadwick.com/wp-content/uploads/2011/05/sample1.jpg" alt="" width="233" height="290" /></a></p>
<p class="DropCaps">Now, this will only color the background of the second item in the list.  Going back to our original problem, we need to color the background of every second item.</p>
<h3>Back to the original problem.</h3>
<p class="DropCaps">This news section will have 9 news items.  However, we don’t want to hard wire our news display to 9.  We may want to change it later on down the line, so it needs to be ready to accept change at any time.</p>
<p>We need a solution that can select every second item in our news list.  So that would be the 2nd, 5th, 8th, etc.  Consider that being the pattern we’ll use.  This pattern will be used over and over again, regardless of how many news items there are.</p>
<h3>Enter the nth-child.</h3>
<p><script class="brush: js" type="syntaxhighlighter"><![CDATA[
 $(document).ready(function() {
	//Background color of the item div: color every 2nd item.
	$(".item:nth-child(3n+2)").css("background-image", "url(images/bg.jpg)");
	});
</script></p>
<p><a href="http://jsfiddle.net/rsadwick/JjRHT/">Click here to see the second example via jsFiddle</a></p>
<p><a href="http://ryan.sadwick.com/wp-content/uploads/2011/05/sample2.jpg"><img class="alignnone size-full wp-image-798" title="sample2" src="http://ryan.sadwick.com/wp-content/uploads/2011/05/sample2.jpg" alt="" width="256" height="293" /></a></p>
<p class="DropCaps">So we’re using 3n+2.  What does that mean, you ask?  Remember our pattern?  <span class="emp">Every</span> second item in our list, regardless of how many news items there are, we will change the background item.&gt;</p>
<p>Think of 3n has our pattern.  Think of 2 as our starting point.  We are coloring the background of the 2nd element right off the bat.  So now we need to get the next item to color.  We’ll use this expression:</p>
<p><strong>(3 x 1) + 2</strong>.</p>
<p>The next item that needs to be colored is 5.  We keep looping through, so to figure out the next item, we’d use:</p>
<p><strong>(3 x 2) + 2</strong>.</p>
<p>That comes out to 8.  I will keep going to illustrate how this expression works.</p>
<p>We keep looping, so 3 x 3 = 9 + 2 = 11.  So the 11th element gets some shadowing love.  So, let’s show our work:</p>
<ul>
<li>(3 x 0) + 2 = 2= 2nd Element</li>
<li>(3 x 1) + 2 = 5= 5th Element</li>
<li>(3 x 2) + 2= 8 = 8th Element</li>
<li>(3 x 3) + 2 = 11th element</li>
<li>and so on.</li>
</ul>
<p>We don’t care how many news items there are total, we can easily change the markup to be dynamic.  So whether your news comes from a database, rss feed, or from wordpress, the visual result will always be the same:  every second item will have the background image changed.</p>
<h3>Why use jQuery instead of pure CSS?</h3>
<p class="DropCaps">The nth child css pseudo-selector is <span class="emp">not</span> supported in Internet Explorer 8 and below.  However, using this jQuery nth child selector, this solution <span class="emp">works</span> IE!</p>
<h3>Source code</h3>
<ul>
<li><a href="http://jsfiddle.net/rsadwick/9QTF7/">Example using UL on jsFiddle</a></li>
<li><a href="http://jsfiddle.net/rsadwick/JjRHT/">Example using Divs on jsFiddle</a></li>
</ul>
</div>
<div class="shr-publisher-762"></div>
<p><!-- Start Shareaholic LikeButtonSetBottom Automatic -->
<div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div>
<div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='standard' data-shr_count='true' data-shr_href='http%3A%2F%2Fryan.sadwick.com%2Fnth-child%2F' data-shr_title='The+Nth+Child+Selector+in+jQuery'></a></div>
<div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div>
<p><!-- End Shareaholic LikeButtonSetBottom Automatic --></p>
]]></content:encoded>
			<wfw:commentRss>http://ryan.sadwick.com/nth-child/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
