<?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>James Wiseman &#187; jQuery</title>
	<atom:link href="http://www.jameswiseman.com/blog/tag/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jameswiseman.com/blog</link>
	<description>My Programming Notebook and Other Things</description>
	<lastBuildDate>Fri, 03 Feb 2012 15:13:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Manually Traverse a DOM Tree Using jQuery</title>
		<link>http://www.jameswiseman.com/blog/2010/08/24/manually-traverse-a-dom-tree-using-jquery/</link>
		<comments>http://www.jameswiseman.com/blog/2010/08/24/manually-traverse-a-dom-tree-using-jquery/#comments</comments>
		<pubDate>Tue, 24 Aug 2010 20:43:44 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[DOM Traversal]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1353</guid>
		<description><![CDATA[Image via Wikipedia jQuery does a wonderful job of traversing the DOM tree, so you might ask why you would like to do this yourself? I recently encountered a requirement to count the maximum &#8216;depth&#8217; of a DOM tree. I.e. find the level of the deepest nested element. So, I managed to knock this up [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><div class="zemanta-img zemanta-action-dragged" style="margin: 1em; display: block;">
<div>
<dl class="wp-caption alignright" style="width: 310px;">
<dt class="wp-caption-dt"><a href="http://commons.wikipedia.org/wiki/File:Dom_tree.png"><img title="Example DOM Tree" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/08/300px-Dom_tree.png" alt="Example DOM Tree" width="300" height="149" /></a></dt>
<dd class="wp-caption-dd zemanta-img-attribution" style="font-size: 0.8em;">Image via <a href="http://commons.wikipedia.org/wiki/File:Dom_tree.png">Wikipedia</a></dd>
</dl>
</div>
</div>
<p>jQuery does a wonderful job of traversing the DOM tree, so you might ask why you would like to do this yourself?<br />
I recently encountered a requirement to count the maximum &#8216;depth&#8217; of a DOM tree. I.e. find the level of the deepest nested element. So, I managed to knock this up in a few minutes:</p>
<pre class="JavaScript" name="code">function Recurse($item, depth) {
    $item.each(function() {
        depth.count++;
        if (depth.count &gt; depth.max) {
            depth.max = depth.count;
        }
        Recurse($(this).children(), depth);
    });
    depth.count--;
    return depth.max;
}

$(document).ready(function() {
    alert(Recurse($("body"), { count: 0, max:0 }));
}</pre>
<p>A few points to note about this</p>
<ul>
<li><strong>depth</strong> is an object with two members, <strong>count</strong> and <strong>max</strong>. We want to keep a count of the current and maximum depth, but want to avoid global variables (it&#8217;s better practice, and makes the solution more self-contained).</li>
<li>JavaScript doesn&#8217;t allow us to pass variables by reference. Using the variables within the passed <strong>depth</strong> object means we can pass values to lower levels of recursion.</li>
<li>We must pass an object in our first call. Technically the function can be improved by including a check for this.</li>
<li>The function returns <strong>depth.max</strong>. Although the return value doesn&#8217;t matter when the function is called recursively, its useful for passing our intended value out to the original calling function.</li>
</ul>
<p>You can test this out on JsFiddle: <a href="http://jsfiddle.net/RqHzf/">http://jsfiddle.net/RqHzf/</a></p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=47ad50c8-ab51-4922-9095-283ac0880bb3" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-1353"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F24%2Fmanually-traverse-a-dom-tree-using-jquery%2F' data-shr_title='Manually+Traverse+a+DOM+Tree+Using+jQuery'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F08%2F24%2Fmanually-traverse-a-dom-tree-using-jquery%2F' data-shr_title='Manually+Traverse+a+DOM+Tree+Using+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/08/24/manually-traverse-a-dom-tree-using-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JSFiddle the Stackoverflow Answer Race</title>
		<link>http://www.jameswiseman.com/blog/2010/07/30/jsfiddle-the-stackoverflow-answer-race/</link>
		<comments>http://www.jameswiseman.com/blog/2010/07/30/jsfiddle-the-stackoverflow-answer-race/#comments</comments>
		<pubDate>Fri, 30 Jul 2010 13:08:07 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Stackoverflow]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=1143</guid>
		<description><![CDATA[Have you ever found yourself wanting to test out a bit of HTML/CSS/JavaScript without having access to an IDE or being bothered to fire it up? Do you want to hack prototype a bit of code quickly without fuss? Or maybe you&#8217;re one of those StackOverflow answerers that Race To Get The First Answer In. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Have you ever found yourself wanting to test out a bit of HTML/CSS/JavaScript without having access to an IDE or being bothered to fire it up?</p>
<p>Do you want to <span style="text-decoration: line-through;">hack </span>prototype a bit of code quickly without fuss?</p>
<p>Or maybe you&#8217;re one of those StackOverflow answerers that <a title="stackoverflow answer race" href="http://meta.stackoverflow.com/questions/58823/feature-for-people-who-race-to-be-the-first-to-answer-questions" target="_blank">Race To Get The First Answer In</a>.</p>
<p>If your answer is &#8216;yes&#8217; or &#8216;I am actually&#8217; then there&#8217;s probably a myriad of different resources you can call upon to help out, but you might consider <a title="JSFilddle" href="http://jsfiddle.net/" target="_blank">JSFiddle </a></p>
<p>I created  a really simple bit of code:</p>
<p><strong>HTML</strong></p>
<pre name="code" class="html">&lt;input type="checkbox" /&gt;</pre>
<p><strong>JavaScript/jQuery</strong></p>
<pre name="code" class="javascript">$("input").click(function(){alert("test");});</pre>
<p>Then I selected &#8216;jQuery 1.4.2.&#8217; from the &#8216;Choose Framework&#8217; section and clicked run.</p>
<p>When I clicked on the checkbox, everything hung together like a dream:<br />
<a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/jsfiddle1.jpg"><img class="alignnone size-full wp-image-1147" title="jsfiddle" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/07/jsfiddle1.jpg" alt="" width="807" height="267" /></a></p>
<p>At the time of writing the blurb stated that it was still under heavy development, however I still thought it was a genuinely nice little tool that I wanted to share.</p>
<div class="zemanta-pixie" style="margin-top: 10px; height: 15px;"><a class="zemanta-pixie-a" title="Enhanced by Zemanta" href="http://www.zemanta.com/"><img class="zemanta-pixie-img" style="border: none; float: right;" src="http://img.zemanta.com/zemified_a.png?x-id=d80bfaa1-e992-4bb9-b3cb-ad2fd00f6a43" alt="Enhanced by Zemanta" /></a><span class="zem-script more-related pretty-attribution"><script src="http://static.zemanta.com/readside/loader.js" type="text/javascript"></script></span></div>
<div class="shr-publisher-1143"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F30%2Fjsfiddle-the-stackoverflow-answer-race%2F' data-shr_title='JSFiddle+the+Stackoverflow+Answer+Race'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F07%2F30%2Fjsfiddle-the-stackoverflow-answer-race%2F' data-shr_title='JSFiddle+the+Stackoverflow+Answer+Race'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/07/30/jsfiddle-the-stackoverflow-answer-race/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>More Biscuits Please</title>
		<link>http://www.jameswiseman.com/blog/2010/05/14/more-biscuits-please/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/14/more-biscuits-please/#comments</comments>
		<pubDate>Thu, 13 May 2010 23:00:24 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Biscuits]]></category>
		<category><![CDATA[Courses and Presentations]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=461</guid>
		<description><![CDATA[Last year I held a jQuery course, and was fortunate enough to recieve very generous praise from my peers, including the following: What parts of the training did you feel were of most value? The overview of the key paragdim interjected with real examples expedited learning What parts of the training did you feel were [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Last year I held a jQuery course, and was fortunate enough to recieve very generous praise from my peers, including the following:</p>
<p><strong>What parts of the training did you feel were of most value?<a href="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/biscuit.jpg"><img class="alignright size-medium wp-image-491" title="biscuit" src="http://www.jameswiseman.com/blog/wp-content/uploads/2010/05/biscuit-300x221.jpg" alt="Biscuits" width="300" height="221" /></a></strong><br />
The overview of the key paragdim interjected with real examples expedited learning</p>
<p><strong>What parts of the training did you feel were of least value?</strong><br />
The lack of biscuits</p>
<p><strong>What, if anything, would you change about the course?</strong><br />
Provide biscuits</p>
<p><strong>Any further comments?</strong><br />
More biscuits please</p>
<p>So there we have it. Biscuits, cakes, muffins, coffees and teas. Bribing your audience can always help!</p>
<div class="shr-publisher-461"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F14%2Fmore-biscuits-please%2F' data-shr_title='More+Biscuits+Please'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F14%2Fmore-biscuits-please%2F' data-shr_title='More+Biscuits+Please'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/14/more-biscuits-please/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Microsoft Is Contributing To jQuery</title>
		<link>http://www.jameswiseman.com/blog/2010/05/11/microsoft-is-contributing-to-jquery/</link>
		<comments>http://www.jameswiseman.com/blog/2010/05/11/microsoft-is-contributing-to-jquery/#comments</comments>
		<pubDate>Tue, 11 May 2010 15:06:14 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Plugins]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=426</guid>
		<description><![CDATA[Back in September 2008 Microsoft announced that it would be shipping jQuery with Visual Studio going forward. This was pretty momentous news at the time, as noted in the jQuery Blog. True to its word, Microsoft Shipped jQuery with Visual Studio 2010, and it is now included by default in your ASP.NET web projects. Further [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Back in September 2008 Microsoft announced that it would be <a title="Microsoft Shipping jQuery with Visual Studio" href="http://weblogs.asp.net/scottgu/archive/2008/09/28/jquery-and-microsoft.aspx" target="_blank">shipping jQuery with Visual Studio going forward</a>. This was pretty momentous news at the time, <a title="jQuery Microsoft Nokia annoucement" href="http://blog.jquery.com/2008/09/28/jquery-microsoft-nokia/" target="_blank">as noted in the jQuery Blog</a>.</p>
<p>True to its word, Microsoft Shipped jQuery with Visual Studio 2010, and it is now included by default in your ASP.NET web projects.</p>
<p>Further to this, Microsoft is now actively <a title="Microsoft contributing to jQuery " href="http://weblogs.asp.net/scottgu/archive/2010/05/07/jquery-templates-and-data-linking-and-microsoft-contributing-to-jquery.aspx" target="_blank">contributing to the jQuery project itself</a>.</p>
<p>All this really underlines the emerging dominance of jQuery and gives extra reason for this to be JavaScript library of choice for anyone thinking of adopting a JavaScript framework.</p>
<div class="shr-publisher-426"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F11%2Fmicrosoft-is-contributing-to-jquery%2F' data-shr_title='Microsoft+Is+Contributing+To+jQuery'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F05%2F11%2Fmicrosoft-is-contributing-to-jquery%2F' data-shr_title='Microsoft+Is+Contributing+To+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/05/11/microsoft-is-contributing-to-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Standards (8) &#8211; Chaining</title>
		<link>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-8-chaining/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-8-chaining/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 15:08:02 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=31</guid>
		<description><![CDATA[Synopsys Use chaining as an alternative to variable caching and multiple selector calls. Where chaining is used, appropriate line breaks and indentation should be used. Do not over-chain. For long chains it acceptable to cache intermediate objects in a variable. Description Chaining is one of the signature features of jQuery. Used correctly, it can reduce [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h3><strong>Synopsys</strong></h3>
<p>Use chaining as an alternative to variable caching and multiple selector calls.</p>
<p>Where chaining is used, appropriate line breaks and indentation should be used.</p>
<p>Do not over-chain. For long chains it acceptable to cache intermediate objects in a variable.</p>
<h3><strong>Description</strong></h3>
<div>Chaining is one of the signature features of jQuery. Used correctly, it can reduce the amount of code we have to write and the amount of data that is held in memory.</div>
<p>Chaining can be considered as an alternative to:</p>
<ul>
<li>Caching jQuery objects in local variables</li>
<li>Performing multiple selections</li>
</ul>
<p>Where chaining is used, appropriate line breaks and indentation should be used.</p>
<h3>When to Use Chains</h3>
<p>Chains should not compromise the readability of the code. For long chains, finding and defining a formatting and indentation strategy can be troublesome.</p>
<p>It can be better in these instances to cache your jQuery object in a separate variable.</p>
<p>When a JavaScript object is cached in jQuery, the reference to the object is actually stored. This occupies a minimal amount of memory, leaving negligible performance degradation.</p>
<p>The items show some indentation strategies, and a representation with a cached variable:</p>
<h3>Chain Formatting Examples</h3>
<p><strong>Long Chains – Indentation Strategy 1</strong></p>
<pre>$(<span style="color: #800000;">"#MyTable"</span>).append(
    objButton.parents(<span style="color: #800000;">"tr"</span>).clone()
        .find(<span style="color: #800000;">".RowTitle"</span>)
            .text(<span style="color: #800000;">"Row "</span> + String(AddCount)).end()
        .find(<span style="color: #800000;">".MySelect1"</span>)
            .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"SomeId" </span>+ String(AddCount))
                .change(<span style="color: #0000ff;">function</span>() { ChangeFundRow() }).end()
        .find(".MySelect2")
            .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"SomeOtherId"</span> + String(AddCount)).end()
);</pre>
<p><strong>Long Chains – Indentation Strategy 2</strong></p>
<pre>$(<span style="color: #800000;">"#FundTable"</span>).append(
    objButton
        .parents(<span style="color: #800000;">"tr"</span>)
            .clone()
                .find(<span style="color: #800000;">".RowTitle"</span>)
            .text(<span style="color: #800000;">"Row "</span> + String(AddCount))
            .end()
                .find(<span style="color: #800000;">".MySelect1"</span>)
                .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"SomeId"</span> + String(AddCount))
                .change(<span style="color: #0000ff;">function</span>() {
                    ChangeFundRow();
                })
            .end()
                .find(<span style="color: #800000;">".MySelect2"</span>)
                .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"SomeOtherId"</span> + String(AddCount))
            .end()
);</pre>
<p>To the untrained eye, either of the above may take some deciphering. In this case, a better strategy could be to cache your selections in a separate variable:</p>
<p><strong>Long Chains – Caching Variable</strong></p>
<pre><span style="color: #0000ff;">var</span> $clonedRow = objButton.parents(<span style="color: #800000;">"tr"</span>).clone();
$clonedRow.find(<span style="color: #800000;">".RowTitle"</span>)
          .text(<span style="color: #800000;">"Row "</span> + nAddCount); 

$clonedRow.find(<span style="color: #800000;">".MySelect1"</span>)
    .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"FundManager"</span> + nAddCount)
    .change( ChangeFundRow ); 

$clonedRow.find(<span style="color: #800000;">".MySelect2"</span>)
          .attr(<span style="color: #800000;">"id"</span>, <span style="color: #800000;">"FundName"</span> + nAddCount); 

$clonedRow.appendTo(<span style="color: #800000;">"#FundTable"</span>);</pre>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-31"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-8-chaining%2F' data-shr_title='jQuery+Standards+%288%29+-+Chaining'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-8-chaining%2F' data-shr_title='jQuery+Standards+%288%29+-+Chaining'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-8-chaining/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery Standards (7) &#8211; Plugin Use</title>
		<link>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-7-plugin-use/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-7-plugin-use/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 14:35:55 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=30</guid>
		<description><![CDATA[Synopsis Do not reinvent the wheel. Use plugins if they fit your functional requirement. Ensure the plugins are fit-for-purpos, well regarded and well tested. Description Plugins are a way by which any developer can extend the jQuery library. They are often useful, frequently well tested and supported, and may well cut down on development time. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><h3>Synopsis</h3>
<p>Do not reinvent the wheel. Use plugins if they fit your functional requirement.</p>
<p>Ensure the plugins are fit-for-purpos, well regarded and well tested.</p>
<p><strong>Description</strong></p>
<p>Plugins are a way by which any developer can extend the jQuery library. They are often useful, frequently well tested and supported, and may well cut down on development time.</p>
<p>The following are some commonly used plugins:</p>
<ul>
<li><strong>Form plugin:</strong> <a rel="nofollow" href="http://malsup.com/jquery/form/">http://malsup.com/jquery/form/</a></li>
<li><strong>BlockUI plugin:</strong> <a rel="nofollow" href="http://malsup.com/jquery/block/#">http://malsup.com/jquery/block/#</a></li>
<li><strong>TableSorter plugin:</strong> <a rel="nofollow" href="http://tablesorter.com/docs/">http://tablesorter.com/docs/</a></li>
<li><strong>Select Box Plugin:</strong> <a rel="nofollow" href="http://www.texotela.co.uk/code/jquery/select/">http://www.texotela.co.uk/code/jquery/select/</a></li>
</ul>
<p><strong>Reviewing Plugins for Use</strong></p>
<p>The review process for including a plugin should encompass some sort of investigation into the plugin itself. The following aspects should be examined:</p>
<ul>
<li>The current level of use<strong> &#8211; </strong> I.e. is it widely adopted? Those with a large number of users will be well tested and developed.</li>
<li>For which version of jQuery was it written? Is it still valid for your current version of jQuery?</li>
<li>The current level of support. E.g. does it have a website with examples, etc. An example of one with good support is BlockUI (<a rel="nofollow" href="http://malsup.com/jquery/block/#">http://malsup.com/jquery/block/#</a>)</li>
<li>Quality of plugin comments &#8211; A quick scan of the code will see if it’s well documented. If its well documented, then it should be well written.</li>
<li>How well is it regarded?. A quick internet search across the jQuery community and its users should reveal this.</li>
<li>Availability of vsdoc js for intellisense. Not critical, but would be a factor in choosing two plugins that were identical to each other in every other aspect.</li>
</ul>
<h3>Writing Plugins</h3>
<p>Whenever you find yourself developing some jQuery that could be considered generic, and could certainly adopted within other areas of the code base, then you should consider turning it into the jQuery plugin.</p>
<p>The ‘how’ of doing this is outside the scope of these standards. Refer to jQuery literature (department books and online) for how to do this. Experienced jQuery developers may also be able to assist.</p>
<p>As a starting point, any plugin should begin with the following <a title="JavaScript Closures" href="http://www.jibbering.com/faq/faq_notes/closures.html" target="_blank">closure</a> code convention:</p>
<pre>    <span style="color: #0000ff;">function</span>($){
        <span style="color: #008000;">//plugin code</span>
    }(jQuery);</pre>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-30"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-7-plugin-use%2F' data-shr_title='jQuery+Standards+%287%29+-+Plugin+Use'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-7-plugin-use%2F' data-shr_title='jQuery+Standards+%287%29+-+Plugin+Use'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-7-plugin-use/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery Standards (6) – jQuery Selectors</title>
		<link>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-6-%e2%80%93-selectors/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-6-%e2%80%93-selectors/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 12:37:56 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=211</guid>
		<description><![CDATA[Synopsis Use #ID selector wherever possible. It is the fastest. Ensure slower selectors are optimised for performance &#8211; Combine them with faster selectors where possible. Custom selectors must be kept simple, as they may end up being run upon every element in the DOM. Avoid needless selector combinations. Description Selectors are ranked by performance as [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsis</strong></p>
<p>Use #ID selector wherever possible. It is the fastest.</p>
<p>Ensure slower selectors are optimised for performance &#8211; Combine them with faster selectors where possible.</p>
<p>Custom selectors must be kept simple, as they may end up being run upon every element in the DOM.</p>
<p>Avoid needless selector combinations.</p>
<p><strong>Description</strong></p>
<p>Selectors are ranked by performance as follows:</p>
<div>
<ol>
<li>#Id</li>
<li>Element</li>
<li>.class, :pseudoclass and :custom</li>
</ol>
</div>
<p>The Class and PseudoClass and Custom selectors are slower than ID and Element selectors. The deficiency of their performance can be mitigated by combining  them with other selector types, so do this wherever possible.</p>
<p><strong>Examples</strong></p>
<pre>    $(<span style="color: #800000;">".oddRows"</span>);                   <span style="color: #008000;">//Inefficient: scans DOM for all elements with oddrows class</span>
    $(<span style="color: #800000;">"tr.oddRows"</span>);                 <span style="color: #008000;">//More efficient: Searches only &lt;tr&gt;s with oddrows class</span>
    $(<span style="color: #800000;">"#MyTable tr.oddRows"</span>);        <span style="color: #008000;">//More efficient: searches descendents of #MyTable</span>
    $(<span style="color: #800000;">"#MyTable&gt;tbody&gt;tr.oddRows"</span>);  <span style="color: #008000;">//Best: searches immediate children</span></pre>
<p>In these examples, we have combined CSS style selectors to  obtain better performance in two ways:</p>
<ul>
<li>Element.WithClass – I.e. search for element with the specified class name</li>
<li>#Id Descendents – I.e. search within descendents of the Id.</li>
<li>#Id&gt;Children – I.e. search only immediate children</li>
</ul>
<p>The optimisation of selector performance is essential for  efficient jQuery. This must be a major consideration for developers and  code-reviewers alike.</p>
<p><strong>Custom Selectors</strong></p>
<p>It is possible within jQuery to define custom selectors, as per the following  example:</p>
<pre>    $.extend($.expr[<span style="color: #800000;">":"</span>], {
        textboxEmpty: <span style="color: #0000ff;">function</span>(el) {
            <span style="color: #0000ff;">return </span>$(el).val() === <span style="color: #800000;">""</span>;  <span style="color: #008000;">//must return boolean</span>
        }
<div>    });</div>
<div>    alert($(<span style="color: #800000;">":text:textboxEmpty"</span>).length);</div>
</pre>
<p>Custom selectors must be kept simple, as they may end  up being run upon every element in the DOM.</p>
<p>Be aware that Custom and Pseudoclass selectors are, at best, as  slow as class selectors (complex custom selectors may be even be slower!).  Therefore you should attempt to optimise their performance by utilising some of  the techniques described above.</p>
<p>For a tutorial on writing custom selectors, check out my blog post <a title="jQuery Custom Selectors Tutorial" href="http://www.jameswiseman.com/blog/?p=70" target="_blank">here</a>.</p>
<h3><strong>Pitfalls and Traps</strong></h3>
<p><strong>Assuming Children and Descendants</strong></p>
<p>The example <span style="color: #800000;">&#8220;#MyTable&gt;tbody&gt;tr.oddRows&#8221; <span style="color: #000000;">helps us </span></span>demonstrates the  potential pitfalls of assuming specific children/descendants. Had we written  this as <span style="color: #800000;">&#8220;#MyTable&gt;tr.oddRows&#8221;</span> then our  selector would have returned nothing!</p>
<p><strong>Correct Use of Parent-Child</strong></p>
<p>The selector $(<span style="color: #800000;">“#MyTable .oddRows”</span>) will return all elements in  the DOM with the oddrows class assigned (there is a space between <span style="color: #800000;">#MyTable</span> and <span style="color: #800000;">.oddRows</span>)</p>
<p>However, the selector $(<span style="color: #800000;">“#MyTable.oddRows”</span>) will probably return  nothing, as the code imples that we are looking for the table with the Id <span style="color: #800000;">#MyTable</span> that has the class  <span style="color: #800000;">oddrows</span> assigned to it.</p>
<p>Given the naming convention in the example it seems unlikely  that that the table will have the class assigned.</p>
<p><strong>Needless Selector Combinations</strong></p>
<p>The Id of an element should be unique. Therefore there is no  need to combine it with other selectors. For example, the selector  $(<span style="color: #800000;">&#8220;#MyTable.TableClass&#8221;</span>) is probably pointless as the query for <span style="color: #800000;">#MyTable</span> will already  have returned at most one element.</p>
<p>In the same vein, the selector $(<span style="color: #800000;">&#8220;div #MyTable&#8221;</span>) is also  pointless. Here we are going to select all divs in the DOM, and then scan that subsection for <span style="color: #800000;">#MyTable<span style="color: #000000;">.</span></span></p>
<p>Defining an Id at the start of a Parent-Child selector can,  however improve selector performance greatly, and is strongly encouraged. See  selector performance examples above.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-211"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-6-%25e2%2580%2593-selectors%2F' data-shr_title='jQuery+Standards+%286%29+%E2%80%93+jQuery+Selectors'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-6-%25e2%2580%2593-selectors%2F' data-shr_title='jQuery+Standards+%286%29+%E2%80%93+jQuery+Selectors'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-6-%e2%80%93-selectors/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>jQuery Standards (5) &#8211; Effects and Animation</title>
		<link>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-5-effects-and-animation/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-5-effects-and-animation/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 10:46:26 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=28</guid>
		<description><![CDATA[Synopsis Adopt a restrained and consistent approach to implementing animation functionality. Description jQuery provides some basic functionality, the simplest of which is demonstrated by the following code: $("#MyDiv").hide("slow"); Here, the hiding of MyDiv is animated. It is important that a consistent approach to the use of effects and anuimation is adopted across the web application. [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsis</strong></p>
<p><span style="font-weight: normal; font-size: 13px;">Adopt a restrained and consistent approach to implementing animation functionality.</span></p>
<p><strong>Description</strong></p>
<div>jQuery provides some basic functionality, the simplest of which is demonstrated by the following code:</div>
<pre>    $(<span style="color: #800000;">"#MyDiv"</span>).hide(<span style="color: #800000;">"slow"</span>);</pre>
<p>Here, the hiding of MyDiv is animated.</p>
<p>It is important that a consistent approach to the use of effects and anuimation is adopted across the web application.</p>
<p>There is a temptation when discovering this functionality to &#8216;go nuts&#8217; and have divs whizzing in from all angles with buttons flashing and all hell breaking loose. In general you should try to show restraint when using animation.</p>
<p>If possible, feed animation use into UI standards.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-28"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-5-effects-and-animation%2F' data-shr_title='jQuery+Standards+%285%29+-+Effects+and+Animation'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-5-effects-and-animation%2F' data-shr_title='jQuery+Standards+%285%29+-+Effects+and+Animation'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-5-effects-and-animation/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>jQuery Standards (4) &#8211; Page Style and Layout Changes</title>
		<link>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-4-page-style-and-layout-changes/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-4-page-style-and-layout-changes/#comments</comments>
		<pubDate>Thu, 22 Apr 2010 08:59:43 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=27</guid>
		<description><![CDATA[Synopsis Avoid making changes to individual CSS styles using the jQuery API functions. Use CSS Classes intead. Description Avoid using direct CSS style changes (using .css() ), and inbuilt style functions .length(), .width(), etc. Instead, declare the styles in a class within a CSS StyleSheet file (not inline within an HTML file!), and use .addClass(), [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsis</strong></p>
<p>Avoid making changes to individual CSS styles using the jQuery API functions.</p>
<p>Use CSS Classes intead.</p>
<p><strong>Description</strong></p>
<p>Avoid using direct CSS style changes (using <code>.css()</code> ), and inbuilt style functions <code>.length()</code>, <code>.width()</code>, etc.</p>
<p>Instead, declare the styles in a class within a CSS StyleSheet file (not inline within an HTML file!), and use .addClass(), .removeClass() or .toggleClass() upon your selecteded object(s).</p>
<p>For example, avoid this type of declaration:</p>
<pre>    $(<span style="color: #800000;">"#MyTR"</span>).css({
       "<span style="color: #800000;">background-color"</span>:<span style="color: #800000;">"gray"
</span>    });</pre>
<p>Use instead:</p>
<pre>    $(<span style="color: #800000;">"#MyTR"</span>).addClass(<span style="color: #800000;">"HighlightRow"</span>);

    <span style="color: #008000;">/*In CSS File:*/
</span>    <span style="color: #800000;">.HighlightRow</span>
    {
       <span style="color: #ff0000;">background-color</span>:<span style="color: #0000ff;">gray</span>;
    }</pre>
<p>This promotes correct separation of content and style.</p>
<p>Embedding style changes in JavaScript/jQuery has the following implications when it comes to rebranding or changing a site&#8217;s theme:</p>
<ul>
<li>It is less likely that a consistent approach has been adopted with regard to styling making the task harder.</li>
<li>A bigger and more complex  impact analysis required to identify files that require changed.</li>
<li>More files require changed.</li>
<li>More files = more testing. As we will be changing JavaScript code, we will have to retest more than we otherwise would have.</li>
</ul>
<p>A discussion on the merits of <a title="JavaScript Behavioral Separation" href="http://www.alistapart.com/articles/behavioralseparation" target="_blank">Behavioral Separation</a> is outside the scope of this article. It may seem a little draconian to advise against using <strong>.css()</strong> and other style-change functions, but is essential when attempting to achieve this goal.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-27"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-4-page-style-and-layout-changes%2F' data-shr_title='jQuery+Standards+%284%29+-+Page+Style+and+Layout+Changes'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F22%2Fjquery-standards-4-page-style-and-layout-changes%2F' data-shr_title='jQuery+Standards+%284%29+-+Page+Style+and+Layout+Changes'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/22/jquery-standards-4-page-style-and-layout-changes/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>jQuery Coding Standards (3) &#8211; Events</title>
		<link>http://www.jameswiseman.com/blog/2010/04/21/jquery-coding-standards-3-events/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/21/jquery-coding-standards-3-events/#comments</comments>
		<pubDate>Wed, 21 Apr 2010 12:40:28 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=26</guid>
		<description><![CDATA[Adopt a consistent approach when writing document ready events.
Use a document ready event in place of window onload.
No JavaScript or behavioural markup is to be included in HTML files.
Events must be late-bound within the $(document).ready()handler code itself.
Use the shorthand event syntax.
Anonymous function handlers must be limited to one line of code.
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsis</strong></p>
<p>1. Adopt a consistent approach when writing document ready events.</p>
<p>2. Use a <a title="jQuery Document Ready" href="http://api.jquery.com/ready/" target="_blank">document ready </a>event in place of window onload.</p>
<p>3. No JavaScript or behavioural markup is to be included in HTML files.</p>
<p>4. Events must be late-bound within the $(document).ready()handler code itself.</p>
<p>5. Use the shorthand event syntax.</p>
<p>6. Anonymous function handlers must be limited to one line of code.</p>
<p><strong>Document Ready Declaration</strong></p>
<p>1. The handler for the <a title="jQuery document.ready declaration " href="http://api.jquery.com/ready/" target="_blank">document ready</a> event must be an anonymous declared in the following form:</p>
<pre>    $(<span style="color: #0000ff;">document</span>).ready(<span style="color: #0000ff;">function</span>(){
    	<span style="color: #008000;">//all initialization</span>
    });</pre>
<p>This is clear, readable, and should be used ahead of other possible invocations of this functionality, which include:</p>
<pre>    $(handler);</pre>
<pre>    $().ready(handler); <span style="color: #008000;">//not recommended</span></pre>
<pre>    $(<span style="color: #0000ff;">document</span>).bind(<span style="color: #800000;">"ready"</span>, handler); <span style="color: #008000;">//not fired if bound after document ready fires</span></pre>
<p>(And, of course, <strong>&#8216;$&#8217;</strong> is interchangeable with <strong>&#8216;jQuery&#8217;</strong>)</p>
<p>This standard stipulates the use of the given format, however the important aspect of this is the recommendation to adopt a consistent approach to writing document ready events.</p>
<p>2. A Document Ready handler must be specified in place of the window.onload event, except in the rare event where this is too soon: <a href="http://blog.arc90.com/2008/05/16/a-jquery-tip-dont-use-jquery/">http://blog.arc90.com/2008/05/16/a-jquery-tip-dont-use-jquery/</a></p>
<p><strong>Note:</strong> There is no limit on the number of document ready handlers that can be associated with a page. Event handlers are added to a stack that will be executed when the document is ready.</p>
<p><strong>Binding and Behavioural Markup</strong></p>
<p><strong></strong><br />
3. No JavaScript or behavioural markup is to be included in HTML files. For example, the red-highlighted code should not be present in HTML any more:</p>
<pre>    &lt;<span style="color: #000000;">input </span>type="button" id="MyButton" <span style="color: #ff0000;">onclick="OnClickMyButton()</span><span style="color: #ff0000;">"</span>&gt;</pre>
<p>4. Events must be late-bound within the $(document).ready()handler code itself. For example:</p>
<pre>    $(<span style="color: #0000ff;">document</span>).ready(<span style="color: #0000ff;">function</span>(){
        $(<span style="color: #800000;">"#MyButton"</span>).click(MyFunc);
    });

    <span style="color: #0000ff;">function</span> MyFunc(event){
    }</pre>
<p>5. Use the shorthand event syntax:</p>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).click(MyFunc); <span style="color: #008000;">//use this</span></pre>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).bind(<span style="color: #800000;">"click"</span>, MyFunc); <span style="color: #008000;">//instead of this</span></pre>
<p><strong>Event Handlers and Anonymous Functions</strong><br />
6. jQuery allows developers to specify anonymous functions for event handlers. For example:</p>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).click(function(){<span style="color: #008000;">//code</span>});</pre>
<p>However, to avoid <em>On Ready Bloating</em> (a side effect of late-binding where the document.ready() function handler becomes large, cumbersome, and unmaintainable), these anonymous function handlers must be limited to one line of code:</p>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).click(<span style="color: #0000ff;">function</span>(){
        MyFunction();
    });</pre>
<p>If we need to refer to the this object, we wrap it in a jQuery object, and pass it within our anonymous function (it is better practice to wrap in a jQuery object early on):</p>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).click(<span style="color: #0000ff;">function</span>(){
        MyFunction($(this));
    });</pre>
<p>jQuery will automatically pass the event object when calling our anonymous function. We can access it as follows:</p>
<pre>    $(<span style="color: #800000;">"#MyButton"</span>).click(function(event){
        MyFunction($(<span style="color: #0000ff;">this</span>), event);
    });</pre>
<p>Incidentally, the declaration for MyFunction for the above example would be:</p>
<pre>    <span style="color: #0000ff;">function </span>MyFunction($this, event){
        <span style="color: #008000;">//code</span>
    }</pre>
<p>As well as avoiding OnReady Bloating, this approach makes our code easier to debug and more readable.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-26"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F21%2Fjquery-coding-standards-3-events%2F' data-shr_title='jQuery+Coding+Standards+%283%29+-+Events'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F21%2Fjquery-coding-standards-3-events%2F' data-shr_title='jQuery+Coding+Standards+%283%29+-+Events'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/21/jquery-coding-standards-3-events/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>jQuery Coding Standards (2) &#8211; DOM Manipulation</title>
		<link>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-2-dom-manipulation/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-2-dom-manipulation/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 15:26:36 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=24</guid>
		<description><![CDATA[Manipulation of the Document Object Model (DOM) can be costly and inefficient, regardless of whether it is undertaken through jQuery or JavaScript. 
The API provided by jQuery facilitates greater DOM manipulation, possibility outwith of the awareness of the developer.
Therefore, you should limit the amount of code that performs DOM Manipulation tasks. ]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsis</strong></p>
<p>Manipulation of the <a title="Document Object Model" href="http://www.w3.org/DOM/#what" target="_blank">Document Object Model </a>(DOM) can be costly and inefficient, regardless of whether it is undertaken through jQuery or JavaScript.</p>
<p>The API provided by jQuery facilitates greater DOM manipulation, possibility outwith of the awareness of the developer.</p>
<p>Therefore, you should limit the amount of code that performs DOM Manipulation tasks.</p>
<p><strong>Description</strong></p>
<p>The challenge here is to recognise:</p>
<ol>
<li>When DOM Manipulation is being undertaken</li>
<li>When it is being done in excess.</li>
</ol>
<p>Both of these are open to interpretation as it is difficult to stipulate exactly what constitutes excessive DOM manipulation.</p>
<p>In general, very little DOM manipulation should really be undertaken. You should be hitting the DOM in this way only when is absolutely necessary.</p>
<p>Let us examine the following example:</p>
<pre class="javascript" name="code">var $myList = $("#myList");   

for (i=0; i&lt;1000; i++){
    $myList.append("This is list item " + i);
}</pre>
<p>This code adds 1000 lines to an HTML list. This is done with 1000 successive calls to the <strong>.append()</strong> method, and hence, 1000 manipulations to the DOM.</p>
<p>The following code, modified from the example above demonstrates how this can be made more efficient:</p>
<pre class="javascript" name="code">var $myList = $("#myList");
var li = "";   

for (i=0; i&lt;1000; i++){
    li += "&lt;li&gt;This is list item " + i + "&lt;/li&gt;";
}  

$myList.append(li);</pre>
<p>In this example, the HTML is constructed in advance. The HTML is appended in one call, greatly reducing the amount of manipulation you undertake.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-24"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-2-dom-manipulation%2F' data-shr_title='jQuery+Coding+Standards+%282%29+-+DOM+Manipulation'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-2-dom-manipulation%2F' data-shr_title='jQuery+Coding+Standards+%282%29+-+DOM+Manipulation'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-2-dom-manipulation/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>jQuery Coding Standards (1) &#8211; jQuery Variables</title>
		<link>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-1-jquery-variables/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-1-jquery-variables/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 12:12:18 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=23</guid>
		<description><![CDATA[All variables that are use to store/cache jQuery objects should have a name prefixed with a ‘$’]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Synopsys</strong></p>
<p>All variables that are use to store/cache jQuery objects should have a name prefixed with a ‘$’.</p>
<p>Consider using chains in place of jQuery object variables.</p>
<p><strong>Description</strong></p>
<p>There is often a requirement to store/cache a jQuery selection in a holding variable. Where this is the case, you should prefix the variable name with a ‘$’.</p>
<p>For example:</p>
<pre>    <span style="color: #0000ff;">var</span> $MyTable = $(<span style="color: #993300;">"#MyTable"</span>);
    $MyTable.addClass(<span style="color: #993300;">"MyTable"</span>);
    $MyTable.append(<span style="color: #993300;">"&lt;tr&gt;&lt;/tr&gt;"</span>);</pre>
<p>Also, in conjunction with $(this):</p>
<pre>    <span style="color: #0000ff;">var</span> $this = $(<span style="color: #0000ff;">this</span>);</pre>
<p>Storing in a holding variable helps reduce the number of calls into jQuery, and enhances performance. The dollar notation on all jQuery-related variables helps us easily distinguish jQuery variables from standard JavaScript variables at a glance (e.g. string, integer, etc).</p>
<p><strong>Cached Variables vs Chains</strong></p>
<p>The above example could have been written efficiently as a chain like so:</p>
<pre>    $(<span style="color: #993300;">"#MyTable"</span>).addClass(<span style="color: #993300;">"MyTable"</span>).append(<span style="color: #993300;">"&lt;tr&gt;&lt;/tr&gt;"</span>);</pre>
<p>This has the advantage in removing the need for a cached variable, and in this particular case makes sense.</p>
<p>When chains become long, complex and especially hard to read, you should split them up, storing intermediate selections in a holding variable: <a rel="nofollow" href="http://stackoverflow.com/questions/1286829/is-there-a-preferred-way-of-formatting-jquery-chains-to-make-them-more-readable">http://stackoverflow.com/questions/1286829/is-there-a-preferred-way-of-formatting-jquery-chains-to-make-them-more-readable</a></p>
<p>This does not result in much (if any) of a performance hit. Objects are held as references, not values, so their impact is minimal: <a rel="nofollow" href="http://stackoverflow.com/questions/1287106/can-it-be-disadvantageous-to-store-jquery-objects-in-a-variable">http://stackoverflow.com/questions/1287106/can-it-be-disadvantageous-to-store-jquery-objects-in-a-variable</a></p>
<p>The <a title="Chaining Standards" href="&lt;a title=">Chaining Standards</a> cover this in more detail.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-23"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-1-jquery-variables%2F' data-shr_title='jQuery+Coding+Standards+%281%29+-+jQuery+Variables'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-1-jquery-variables%2F' data-shr_title='jQuery+Coding+Standards+%281%29+-+jQuery+Variables'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-1-jquery-variables/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>&#8220;I Don&#8217;t Think jQuery Should Be Constrained By Standards&#8221;</title>
		<link>http://www.jameswiseman.com/blog/2010/04/20/i-dont-think-jquery-should-be-constrained-by-standards/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/20/i-dont-think-jquery-should-be-constrained-by-standards/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 12:10:55 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=49</guid>
		<description><![CDATA[Yes, maybe, and yet, a few days ago I code reviewed a bit of code like this: $("select[id='MySelect']").val(1); Where, of course, an ID selector would have sufficed, and would have been faster, smaller and better: $("#MySelect").val(1); jQuery isn’t a silver bullet against bad code. You can still write some quite horrific jQuery, and moreover, as [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>Yes, maybe, and yet, a few days ago I code reviewed a bit of code like this:</p>
<pre class="JavaScript" name="code">$("select[id='MySelect']").val(1);</pre>
<p>Where, of course, an ID selector would have sufficed, and would have been faster, smaller and better:</p>
<pre class="JavaScript" name="code">$("#MySelect").val(1);</pre>
<p>jQuery isn’t a silver bullet against bad code. You can still write some quite horrific jQuery, and moreover, as JavaScript isn’t subject to precompilation you can ship syntactically incorrect jQuery.</p>
<p>If this were not enough, jQuery, and in particular the <a href="http://sizzlejs.com" target="_blank">Sizzle Selector Engine</a> does a lot ‘under the hood’ with regard to DOM manipulation and traversal. The consequence of this is that, although you code may look great, and be perfectly bug free, it may grind to a painful halt under real-world stupid-user scrutiny.</p>
<p>Standards can help guard against all of this. I’m not really wanting to get tried up in a discussion on the virtues of standards, but, in general, if you believe in them elsewhere, then they are equally valid here.</p>
<p><strong>jQuery Coding Standards Menu</strong></p>
<ul>
<li><a title="jQuery Coding Standards Home" href="http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/">jQuery Coding Standards Home</a></li>
<li><a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">I Don’t Think jQuery Should Be Constrained By Standards</a></li>
</ul>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<div class="shr-publisher-49"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fi-dont-think-jquery-should-be-constrained-by-standards%2F' data-shr_title='%22I+Don%27t+Think+jQuery+Should+Be+Constrained+By+Standards%22'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fi-dont-think-jquery-should-be-constrained-by-standards%2F' data-shr_title='%22I+Don%27t+Think+jQuery+Should+Be+Constrained+By+Standards%22'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/20/i-dont-think-jquery-should-be-constrained-by-standards/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>jQuery Coding Standards and Best Practice</title>
		<link>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/#comments</comments>
		<pubDate>Tue, 20 Apr 2010 12:05:05 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Coding Standards]]></category>
		<category><![CDATA[Standards and Best Practice]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=48</guid>
		<description><![CDATA[The jQuery (http://jquery.com/) JavaScript library has established itself as the most dominant and widely-used JavaScript library available.

And yet, for me, there no sites that appear to pull together best practices for jQuery development in a way that Douglas Crockford (http://www.crockford.com/) has done for JavaScript (http://javascript.crockford.com/code.html)
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p><strong>Overview</strong></p>
<p>The <a title="jQuery JavaScript Library" href="http://jquery.com/" target="_blank">jQuery JavaScript library</a> has established itself as the most dominant and widely-used JavaScript library available.</p>
<p>And yet, for me, there no sites that appear to pull together best practices for jQuery development in a way that <a title="Douglas Crockford" href="http://www.crockford.com/" target="_blank">Douglas Crockford</a> has done with his <a title="JavaScript Code Conventions" href="http://javascript.crockford.com/code.html" target="_blank">JavaScript Code Conventions</a>.</p>
<p>So, while researching the language for the rollout of a jQuery course, I found myself needing to develop a set of standards.</p>
<p>Some time on from this, lessons have been learned and I feel I have a good workable set of jQuery standards. I have decided to present these in a series of posts that fall under the following headers:</p>
<ol>
<li><a title="jQuery Standards - jQuery Variables" href="http://www.jameswiseman.com/blog/?p=23" target="_blank">jQuery Variables</a></li>
<li><a title="jQuery Standards - DOM Manipulation" href="http://www.jameswiseman.com/blog/?p=24" target="_blank">DOM Manipulation</a></li>
<li><a title="jQuery Coding Standards - Events" href="http://www.jameswiseman.com/blog/?p=26" target="_blank">Events</a></li>
<li><a title="jQuery Coding Standards - Page Style and Layout Changes" href="http://www.jameswiseman.com/blog/?p=27" target="_blank">Page Style and Layout Changes</a></li>
<li><a title="jQuery Coding Standards - Effects and Animation" href="http://www.jameswiseman.com/blog/?p=28" target="_blank">Effects and Animation</a></li>
<li><a title="jQuery Coding Standards - Selectors" href="http://www.jameswiseman.com/blog/?p=211" target="_blank">Selectors</a></li>
<li><a title="jQuery Coding Standards - Plugins" href="http://www.jameswiseman.com/blog/?p=30" target="_blank">Plugins</a></li>
<li><a title="jQuery Coding Standards - Chaining" href="http://www.jameswiseman.com/blog/?p=31" target="_blank">Chaining</a></li>
</ol>
<p><strong>Scope</strong></p>
<p>The initial scope of these posts is intended to cover coding from the core jQuery library. only. It is not intended to cover the use of jQuery UI, or other high-profile plugins.</p>
<p>Such is the significance and widespread adoption of some of these add-ons, for example the  <a title="jQuery Forms Validator" href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">jQuery forms validator</a>, or anything that <a title="malsup jQuery Plugins" href="http://malsup.com/jquery/" target="_blank">malsup</a> has written,  that a set of good practice guidelines for these may also be of benefit. But not here, not now at least.</p>
<p><strong>Audience</strong></p>
<p>These standards were developed following the rollout of jQuery within a corporate development community.</p>
<p>They were a result of capturing developer experiences, and evolved over a number of iterations to suit the ecosystem of a very specific development community.</p>
<p>I do believe, however, that they have a broad relevance and use, and will serve any jQuery developer well.</p>
<p><strong>jQuery Version</strong></p>
<p><strong><span style="font-weight: normal;">These standards were developed against jQuery 1.4.2.</span></strong></p>
<p><strong>I Don’t Think jQuery Should / Needs To be constrained </strong></p>
<p>I disagree, which is why I&#8217;ve written these standards. I&#8217;ve felt this warrants a <a title="I Don’t Think jQuery Should Be Constrained By Standards" href="http://www.jameswiseman.com/blog/?p=49" target="_blank">separate article</a>.</p>
<p><strong>I think you/a standard/everything here is wrong</strong></p>
<p>And you may be correct.</p>
<p>I’m happy to change, debate and listen to any opinions to this end, but I would encourage you to bear in mind the audience, and motivation for this. The standards were developed for use within a corporate programming environment, and although I’ve always tried to carry these into my everyday programming adventures, I realise they are not for everyone.</p>
<p>For me, standards are there to make you think about what you are doing. On occasions I think standards can be broken, but these scenarios will be exceptions, not rules. And if you are thinking about deviating from a standards, so you should be thinking about commenting. For a well disciplined programmer, good standards lead to better code and better comments.</p>
<p>You may also feel that I’ve missed something. Quite possibly – this was evolved over a number of months in response the practices adopted by a specific development community. I hope for these standards to evolve now they have been exposed to the outside world!</p>
<div class="shr-publisher-48"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-and-best-practice%2F' data-shr_title='jQuery+Coding+Standards+and+Best+Practice'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F20%2Fjquery-standards-and-best-practice%2F' data-shr_title='jQuery+Coding+Standards+and+Best+Practice'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/20/jquery-standards-and-best-practice/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Creating a Custom Selector in jQuery</title>
		<link>http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/</link>
		<comments>http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 12:42:11 +0000</pubDate>
		<dc:creator>jameswiseman</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[custom-selectors]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.jameswiseman.com/blog/?p=70</guid>
		<description><![CDATA[For me, one of the best, yet under-utilised feature of jQuery is the custom selector. 
Custom selectors are another way to extend jQuery for your own means, and to hive off worker functionality that needn't clutter your code.
]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>For me, one of the best, yet under-utilised features of jQuery is the custom selector.</p>
<p>Custom selectors are another way to extend jQuery for your own means, and to hive off worker functionality that needn&#8217;t clutter your code.</p>
<h3><span style="font-size: 15px; font-weight: bold;">The Old Way</span></h3>
<p>To demonstrate this, I&#8217;m going to use a simple example for identifying elements with empty <strong>value</strong> attributes. Consider the scenario where you want a bit of client-side script to alert you if there are any empty textboxes upon form submission:</p>
<pre class="javascript" name="code">var i = 0;
$("input:text").each(function() {
    if ($(this).val() === "") {
        i++;
    }
});

if (i &gt; 0) {
    alert("Not all fields filled out");
}</pre>
<p>Now, at face-value then this is a quite valid way of doing things. However, for me, this is a worker task &#8211; a task that in theory can be hived away elsewhere so as not to clutter your code. We can of course, make it into a function:</p>
<pre class="javascript" name="code">function HasEmpty(selector)
    var i = 0;
    $("input:text").each(function() {
        if ($(this).val() === "") {
            i++;
        }
    });
    return (i &gt; 0);
}</pre>
<p>But this really isn&#8217;t all that flexible. Do you want to return the count of items? Or just a Boolean indicating if any are empty? And it&#8217;s not really all that jQuery-like.</p>
<h3>The New Way</h3>
<p>So, how can we make this a little more elegant? By using custom selectors, of course. In fact, we will just be reusing a language feature of jQuery that jQuery itself uses.</p>
<p>Have you ever wondered how jQuery implements the <a title="CSS Pseudo-Class Selectors" href="http://www.w3.org/TR/CSS2/selector.html#pseudo-elements" target="_blank">CSS Pseudo-Class Selectors</a> such as <a title="jQuery :first-child selector" href="http://api.jquery.com/first-child-selector/" target="_blank"><strong>:first-child</strong> </a>or <strong><a title="jQuery :hidden selector" href="http://api.jquery.com/hidden-selector/" target="_blank">:hidden</a></strong>, etc? Well, under the hood, these are all really just custom selectors built into the jQuery framework. This goes for all of the 30+ Pseudo-Class selectors in jQuery.</p>
<p>To do this, we make use of the little known property <strong>jQuery.expr[':'] </strong>in conjunction with <a title="jQuery Extend Function" href="http://api.jquery.com/jQuery.extend/" target="_blank"><strong>jQuery.extend</strong></a> like so:</p>
<pre class="javascript" name="code">$.extend($.expr[":"], {selectorName:[anonymous function]});</pre>
<p>The interesting part of this is <strong>[anonymous function]</strong>. This function will be called for every element against which this selector runs (much in the same way that .<strong>each </strong>works).</p>
<p>If the function returns <span style="color: #0000ff;">&#8220;<strong>true&#8221;</strong></span><strong> </strong>then the current element will be included in the result set. Conversely, a return value of false  <span style="color: #0000ff;">&#8220;</span><span style="color: #0000ff;"><strong>false&#8221;</strong></span> will exclude it. This function is only really useful if we can reference each object upon which it works. We can use this by using the basic definition of this function (there is an extended one which we will discuss later). This accepts a DOM object representing the current element as a parameter.</p>
<p>When referring to the jQuery core, we see that it adopts the convention of naming the parameter <strong>elem</strong>:</p>
<pre class="javascript" name="code">enabled: function(elem){
    return elem.disabled === false &amp;&amp; elem.type !== "hidden";
},

disabled: function(elem){
    return elem.disabled === true;
},</pre>
<p>Who are we to argue? So now, our Custom Selector template becomes:</p>
<pre class="javascript" name="code"> $.extend($.expr[":"], {
    selectorName:function(elem){
        return [boolean expression];
    }
});</pre>
<p>So, returning to our original example of wanting to search for all elements with an empty <strong>val</strong> attribute, our code takes on the following form.</p>
<pre class="javascript" name="code">$.extend($.expr[':'], {
    valueEmpty: function(elem) {
        var $elem= $(elem);
        return ($elem.val() === "") &amp;&amp; ($elem.attr("type") === "text");
    }
});</pre>
<p>I&#8217;ve added a check to ensure the DOM element is a text box, and I&#8217;ve cached the jQuery cast of <strong>elem</strong> to avoid multiple calls into the library. Really, the implementation is secondary to the discussion here. The important thing to note is that it will return a Boolean. Calling out new code is trivial, and as we would expect:</p>
<pre class="javascript" name="code">alert($(":valueEmpty").length);</pre>
<p>Although, I always advocate combining low-performance selectors (of which this is one) with higher performance ones (more on this later):</p>
<pre class="javascript" name="code">alert($("input:valueEmpty").length);</pre>
<h3>Parameterised Custom Selectors</h3>
<p>You&#8217;ll notice that some Pseudo-Class selectors accept a parameter, for example <strong><a href="http://api.jquery.com/has-selector/">:has(selector)</a></strong>. So how do we get some of that good stuff? We are going to build a <strong>lengthBetween</strong> selector that will filter out items whose length is greater than two specified parameters, in this case, four and eight. The calling code will look like:<strong> </strong></p>
<pre class="javascript" name="code">alert($("input:lengthBetween(4,8)").length);</pre>
<p>The anonymous function we specified for our original custom selector actually has four parameters. We only used the first, <strong>elem</strong>. The following is the full definition:</p>
<pre class="javascript" name="code">$.extend($.expr[":"], {selectorName:function(elem, i, match, array){
    return [boolean expression];
});</pre>
<p>So what are all of these parameters?</p>
<ul>
<li><strong>elem </strong>we are already familiar with - it is the current DOM element of the iteration stack</li>
<li><strong>i</strong> is the index of <strong>elem </strong>upon the stack</li>
<li><strong>match </strong>is an array that contains all information about the custom selector</li>
<li><strong>array</strong> contains all the elements in the stack over which we are iterating</li>
</ul>
<p>We are interested in the <strong>match</strong> array parameter, as this is where we will retrieve parameter information. The <strong>match</strong> array stores four pieces of information. So, given the the selector call:</p>
<pre class="javascript" name="code">alert($("input:lengthBetween(4,8)").length);</pre>
<ul>
<li><strong>match[0]</strong> &#8211; contains the full pseudo-class selector call. In this example <strong><span style="color: #993300;">:<span style="color: #800000;">lengthBetween</span><span style="color: #800000;">(4,8)</span></span></strong></li>
<li><strong><strong>match</strong>[1]</strong> &#8211; contains the selector name only. In this example <strong><span style="color: #800000;">lengthBetween</span></strong></li>
<li><strong><strong>match</strong>[2]</strong> &#8211; denotes which, if any, type of quotes are used in the parameter expression. i.e. single (&#8216;)  or double (&#8220;). In this example it will be empty.</li>
<li><strong><strong>match</strong>[3]</strong> &#8211; gives us the parameters, i.e. what is contained in the brackets. In this example <span style="color: #800000;"><strong>4,8</strong></span></li>
</ul>
<p>So, if we just want the parameters, then <strong>match</strong><strong>[3]</strong> is the way forward. This is a simple string value and it&#8217;s really up to you how you handle it. You can String.Split() it to get multiple parameters, and you can consider validating the parameters somehow. Again, we are drifting away somewhat from the scope of this discussion, so I&#8217;ll refer to the jQuery core once more:</p>
<pre class="javascript" name="code">gt: function(elem, i, match){
    return i &gt; match[3] - 0;
},</pre>
<p>Here, a single parameter is expected and assumed. No validation attempt is made. So, returning to our <span style="color: #800000;">:<strong>lengthBetween</strong></span> selector, the implementation might look something like:</p>
<pre class="javascript" name="code">$.extend($.expr[":"], {
    lengthBetween: function(elem, i, match) {
        var params = match[3].split(",");  //split our parameter string by commas
        var elemLen = $(elem).val().length;   //cache current element length
        return ((elemLen &gt;= params[0] - 0) &amp;&amp; (elemLen &lt;= params[1] - 0));  //perform our check
    }
});</pre>
<p>So there we have it - A custom selector that will filter out values whose length is between one and three characters.</p>
<h3><strong>Speed and Efficiency</strong></h3>
<p>I have always encouraged developers to consider the speed and efficiency of their basic selectors. Although it is not an exact science (relative speeds can differ between browsers, and selector combinations can be unpredictable), I have always advocated the following rule of thumb when it comes to using your selectors:</p>
<ol>
<li>An <strong>#ID </strong>selector is quicker than <strong>element </strong>selector is quicker than <strong>.class</strong> selector.</li>
<li>Always try to prefix <strong>.class </strong>selectors with an <strong>element</strong> selector</li>
<li>#ID selectors almost always can be used in isolation.</li>
</ol>
<p>For further reading, Hugo Vidal Teixeira has provided a nice analysis of <a title="jQuery Selector Performance Analysis" href="http://www.componenthouse.com/article-19" target="_blank">jQuery selector performance</a>.</p>
<p>So where do pseudo-class and custom selectors fit into this?</p>
<p>An <strong>#ID</strong> and <strong>element</strong> selector map directly to the core JavaScript functions .<strong>getElementById()</strong> and .<strong>getElementsByTagName</strong>().</p>
<p><strong>Class</strong>, <strong>Pseudo-Class and Custom </strong>selectors all require a scan of the DOM to filter out elements, which is inherently slow. So, treat custom selectors like class <strong>Class </strong>and <strong>Pseudo-Class </strong>selectors when it comes to performance.</p>
<h3><strong>Resources / Further Reading</strong></h3>
<p>Some useful links for further reading and other custom selectors.</p>
<ul>
<li><a href="http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/">http://james.padolsey.com/javascript/extending-jquerys-selector-capabilities/</a></li>
<li><a href="http://code.google.com/p/aost/wiki/CustomJQuerySelectorInTellurium">http://code.google.com/p/aost/wiki/CustomJQuerySelectorInTellurium</a></li>
<li><a href="http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html">http://jquery-howto.blogspot.com/2009/06/jquery-custom-selectors-with-parameters.html</a></li>
</ul>
<h3><strong>And Finally&#8230;</strong></h3>
<p>What of <strong>jQuery.expr[':']</strong>?<strong> </strong> The eagle eyed among you might have questioned as to why we should be constrained to just overriding the colon? In my opinion, this is straying into dangerous territory, however it is possible given the versatility of the Sizzle selector engine.</p>
<p>You can read more in <a title="Qualified Selectors In jQuery" href="http://ejohn.org/blog/qualified-selectors-in-jquery/" target="_self">this article</a>, which documents how to override the &#8216;&lt;&#8217; symbol.</p>
<div class="shr-publisher-70"></div><!-- 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-fblike' data-shr_layout='button_count' data-shr_showfaces='false' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F19%2Fcreating-a-jquery-custom-selector%2F' data-shr_title='Creating+a+Custom+Selector+in+jQuery'></a><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Fwww.jameswiseman.com%2Fblog%2F2010%2F04%2F19%2Fcreating-a-jquery-custom-selector%2F' data-shr_title='Creating+a+Custom+Selector+in+jQuery'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	<img style='margin:0;padding:0;border:0;' width='1px' height='1px' src="http://www.jameswiseman.com/blog/wp-content/plugins/mystat/mystat.php?act=time_load&id=616988&rnd=117575565" /></channel>
</rss>

