<?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>Polymorphism &#187; jQuery</title>
	<atom:link href="http://www.clearcrystalmedia.com/pm/category/programming/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.clearcrystalmedia.com/pm</link>
	<description>using the right technology at the right time</description>
	<lastBuildDate>Fri, 23 Jul 2010 13:27:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Wheels is only a hammer</title>
		<link>http://www.clearcrystalmedia.com/pm/wheels-is-only-a-hammer/</link>
		<comments>http://www.clearcrystalmedia.com/pm/wheels-is-only-a-hammer/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 10:49:23 +0000</pubDate>
		<dc:creator>Chris Peters</dc:creator>
				<category><![CDATA[ColdFusion on Wheels]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[cfml]]></category>
		<category><![CDATA[cfwheels]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[coldfusion on wheels]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://www.clearcrystalmedia.com/pm/?p=385</guid>
		<description><![CDATA[<p>My posting this is a seriously delayed reaction. But I must admit that I was using Wheels as a hammer and trying to treat a problem like a nail when I wrote about <a href="http://www.clearcrystalmedia.com/pm/wheels-jquery-partial-form-focus/">creating a Wheels partial for setting form focus</a>.</p>
<p>Read on to see where I went wrong.</p>]]></description>
			<content:encoded><![CDATA[<p>My posting this is a seriously delayed reaction. But I must admit that I was using Wheels as a hammer and trying to treat a problem like a nail when I wrote about <a href="http://www.clearcrystalmedia.com/pm/wheels-jquery-partial-form-focus/">creating a Wheels partial for setting form focus</a>.</p>
<p>Stephen came up with a much nicer jQuery-only solution for setting form focus:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span>”.<span style="color: #000066;">focus</span>”<span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So now you can set form focus on load by setting that element&#8217;s class to <code>focus</code>. No need to create a Wheels partial and set a variable in your <abbr title="ColdFusion Markup Language">CFML</abbr>.</p>
<p>In my newbness, I wasn&#8217;t thinking that you could have that <code>$(".focus")</code> hanging there without it applying to something on every page. Now I am schooled.</p>
<p>Thanks Stephen!</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/cfml' rel='tag' target='_self'>cfml</a>, <a class='technorati-link' href='http://technorati.com/tag/cfwheels' rel='tag' target='_self'>cfwheels</a>, <a class='technorati-link' href='http://technorati.com/tag/ColdFusion' rel='tag' target='_self'>ColdFusion</a>, <a class='technorati-link' href='http://technorati.com/tag/coldfusion+on+wheels' rel='tag' target='_self'>coldfusion on wheels</a>, <a class='technorati-link' href='http://technorati.com/tag/html' rel='tag' target='_self'>html</a>, <a class='technorati-link' href='http://technorati.com/tag/javascript' rel='tag' target='_self'>javascript</a>, <a class='technorati-link' href='http://technorati.com/tag/jQuery' rel='tag' target='_self'>jQuery</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.clearcrystalmedia.com/pm/wheels-is-only-a-hammer/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Wheels + jQuery partial for setting form focus</title>
		<link>http://www.clearcrystalmedia.com/pm/wheels-jquery-partial-form-focus/</link>
		<comments>http://www.clearcrystalmedia.com/pm/wheels-jquery-partial-form-focus/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 01:33:30 +0000</pubDate>
		<dc:creator>Chris Peters</dc:creator>
				<category><![CDATA[ColdFusion on Wheels]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ccm]]></category>
		<category><![CDATA[cfwheels]]></category>
		<category><![CDATA[chris peters]]></category>
		<category><![CDATA[clear crystal media]]></category>
		<category><![CDATA[ColdFusion]]></category>
		<category><![CDATA[coldfusion on wheels]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.clearcrystalmedia.com/pm/?p=297</guid>
		<description><![CDATA[<p>You’ve seen it before: you get to a login screen, and the cursor automatically jumps to the username box so you can just start typing. It’s very convenient.</p>
<p>But developers seem to screw this up regularly. I’ve seen it implemented so poorly that it becomes highly unusable and opens potential security problems.</p>
<p>I'll take some time to show you how I implemented this in jQuery and then encapsulated it for reuse within a partial in ColdFusion on Wheels.</p>]]></description>
			<content:encoded><![CDATA[<p><strong>Update:</strong> I definitely recommend reading through this, but I was a little flawed in my implementation, as Stephen pointed out in the comments. Read <a href="http://www.clearcrystalmedia.com/pm/wheels-is-only-a-hammer/" title="Wheels is only a hammer">my follow-up</a> for more details.</p>
<p>You&#8217;ve seen it before: you get to a login screen, and the cursor automatically jumps to the username box so you can just start typing. It&#8217;s very convenient.</p>
<p>But developers seem to screw this up regularly. I&#8217;ve seen it implemented so poorly that it becomes highly unusable and opens potential security problems.</p>
<p>On my bank&#8217;s website, for example, the focus will jump to the username field if you&#8217;ve started typing the password before the page has fully loaded. (Sometimes the home page will load slowly, which happens from time to time, you know?) This causes me to expose part of my password to anyone who may be looking over my shoulder!</p>
<h2>jQuery to the rescue</h2>
<p>As I pointed out in my post about my <a title="My first usage of jQuery" href="http://www.clearcrystalmedia.com/pm/my-first-usage-jquery/">first learnings of jQuery</a>, the framework provides a method called <a href="http://docs.jquery.com/Events/ready"><code>ready()</code></a>, which basically helps you to load events as soon as the page is ready for them.</p>
<p>So we can write this simple jQuery code:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#my-form-element&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This causes focus to jump to the form element with an <code>id</code> of <code>my-form-element</code> on page load.</p>
<p>This won&#8217;t cause cursor-jumping mishaps if the user starts typing before the page is ready. (Well, because the page will be ready before the user starts typing. Trippy&#8230;)</p>
<h2>Making this reusable in ColdFusion on Wheels</h2>
<p>I have a couple <a title="Using Layouts" href="http://www.cfwheels.org/docs/chapter/using-layouts">layouts</a> in my current Wheels application that could use this functionality. So I created this <a href="http://www.cfwheels.org/docs/chapter/partials">partial</a> that can be included in both layouts via <code>includePartial()</code>.</p>
<p>This is the partial at <kbd>views/_layout_focus.cfm</kbd>:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsetting</span> enablecfoutputonly<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;true&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfparam</span> <span style="color: #0000FF;">name</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;arguments.focus&quot;</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;string&quot;</span> <span style="color: #0000FF;">default</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;&quot;</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfif</span> <span style="color: #0000FF;">Len</span><span style="color: #0000FF;">&#40;</span>arguments.focus<span style="color: #0000FF;">&#41;</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span>
        <span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #000000; font-weight: bold;">script</span> <span style="color: #0000FF;">type</span><span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;text/javascript&quot;</span><span style="color: #0000FF;">&gt;</span></span>
            $(document).ready(function(){
                $(&quot;<span style="color: #0000FF;">#arguments.focus#</span>&quot;).focus();
            });
        <span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #000000; font-weight: bold;">script</span><span style="color: #0000FF;">&gt;</span></span>
    <span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfoutput</span><span style="color: #0000FF;">&gt;</span></span>
<span style="color: #333333;"><span style="color: #0000FF;">&lt;/</span><span style="color: #990000; font-weight: bold;">cfif</span><span style="color: #0000FF;">&gt;</span></span>
&nbsp;
<span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfsetting</span> enablecfoutputonly<span style="color: #0000FF;">=</span><span style="color: #009900;">&quot;false&quot;</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>This allows me to put this line in the <code>&lt;head&gt;</code> section of my layout files:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!--- Note: This should be &lt;cfparam&gt;'ed earlier in the file, of course ---&gt;</span>
#includePartial(name=&quot;/layout_focus&quot;, focus=layout.focus)#</pre></div></div>

<p>Now whenever one of my views need to put focus on a particular form input, I can add this call to the view file:</p>

<div class="wp_syntax"><div class="code"><pre class="cfm" style="font-family:monospace;"><span style="color: #333333;"><span style="color: #0000FF;">&lt;</span><span style="color: #990000; font-weight: bold;">cfset</span> layout.focus <span style="color: #0000FF;">=</span> <span style="color: #009900;">&quot;##my-form-element&quot;</span><span style="color: #0000FF;">&gt;</span></span></pre></div></div>

<p>Pretty flexible and available whenever it&#8217;s needed.</p>
<p>Most of the time, the view template will set <code>layout.focus</code>, but it&#8217;s also available when the controller needs to do the thinking. Sometimes the controller may need to make a decision like putting focus on the username field vs. the password field, for example. Maybe the username field is already filled in by a cookie or previous user input.</p>
<p>I hope this helps you better understand some of these jQuery and Wheels concepts. I&#8217;m trying to think of interesting real-world examples of where I&#8217;ve started to use this new stuff. I&#8217;ll keep trying until you guys start showering me with praise in the comments. <img src='http://www.clearcrystalmedia.com/pm/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/ccm' rel='tag' target='_self'>ccm</a>, <a class='technorati-link' href='http://technorati.com/tag/cfwheels' rel='tag' target='_self'>cfwheels</a>, <a class='technorati-link' href='http://technorati.com/tag/chris+peters' rel='tag' target='_self'>chris peters</a>, <a class='technorati-link' href='http://technorati.com/tag/clear+crystal+media' rel='tag' target='_self'>clear crystal media</a>, <a class='technorati-link' href='http://technorati.com/tag/ColdFusion' rel='tag' target='_self'>ColdFusion</a>, <a class='technorati-link' href='http://technorati.com/tag/coldfusion+on+wheels' rel='tag' target='_self'>coldfusion on wheels</a>, <a class='technorati-link' href='http://technorati.com/tag/jQuery' rel='tag' target='_self'>jQuery</a>, <a class='technorati-link' href='http://technorati.com/tag/polymorphism' rel='tag' target='_self'>polymorphism</a>, <a class='technorati-link' href='http://technorati.com/tag/web+development' rel='tag' target='_self'>web development</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.clearcrystalmedia.com/pm/wheels-jquery-partial-form-focus/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My first usage of jQuery</title>
		<link>http://www.clearcrystalmedia.com/pm/my-first-usage-jquery/</link>
		<comments>http://www.clearcrystalmedia.com/pm/my-first-usage-jquery/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 23:40:39 +0000</pubDate>
		<dc:creator>Chris Peters</dc:creator>
				<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ccm]]></category>
		<category><![CDATA[chris peters]]></category>
		<category><![CDATA[clear crystal media]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[polymorphism]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web development]]></category>

		<guid isPermaLink="false">http://www.clearcrystalmedia.com/pm/?p=248</guid>
		<description><![CDATA[<p><img src="http://www.clearcrystalmedia.com/pm/wp-content/uploads/jquery-logo.jpg" alt="jQuery: write less, do more" width="215" height="53" class="size-full wp-image-263" /></p>
<p>Finally forcing myself to learn a much-needed skill, I took a dive into learning jQuery. I used jQuery to improve some JavaScript code that I had written a couple months ago.</p>
<p>Read on to see the original code and how much cleaner jQuery made it.</p>]]></description>
			<content:encoded><![CDATA[<p><img class="size-full wp-image-263" src="http://www.clearcrystalmedia.com/pm/wp-content/uploads/jquery-logo.jpg" alt="jQuery: write less, do more" width="215" height="53" /></p>
<p>As I&#8217;ve gotten back into more hardcore development on a project, I decided to finally take <a href="http://www.jquery.com/">jQuery</a> for a spin. I know, this confession makes me look like I&#8217;ve been programming in the early 2000s, but at least I&#8217;ve arrived in current times now.</p>
<h2>The task: hidden links that appear on hover</h2>
<p>In a user management system I&#8217;ve been developing, I wanted to mimic a certain behavior in WordPress. Basically, when you&#8217;re hover over a record in an <abbr title="Hypertext Markup Language">HTML</abbr> table, WordPress will reveal Edit, Delete, etc. links. Then when you mouse out of that table row, the links go away.</p>
<p style="text-align: center;"><img class="alignnone size-full wp-image-255" title="WordPress Hover" src="http://www.clearcrystalmedia.com/pm/wp-content/uploads/wordpress-hover.jpg" alt="WordPress Hover" width="241" height="122" /></p>
<p>As a final note, the links also need to be invisible on page load.</p>
<h2>My first attempt at the JavaScript</h2>
<p>A couple months ago, I wrote this script in plain-vanilla JavaScript, which mainly got the job done. Let&#8217;s not look too hard at this other than to see how verbose it is.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// UsersAndGroups object creates namespace to work in</span>
Editable <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Object<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Initialize interface</span>
Editable.<span style="color: #660066;">initInterface</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Hide all util links</span>
    Editable.<span style="color: #660066;">hideUtilLinks</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Hides edit and delete links until hover</span>
Editable.<span style="color: #660066;">hideUtilLinks</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> children <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'util-links'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Hide all group util links</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> children.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'util-links invisible-with-javascript'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Shows edit and delete links on hover</span>
Editable.<span style="color: #660066;">showUtilLinks</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> utilLinks <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'util-links'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> utilLinks.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        utilLinks<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">className</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'util-links'</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Add hover and mouseout events for table rows</span>
Editable.<span style="color: #660066;">initUtilLinkBehavior</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> tables <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByClassName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'editable'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> children <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> tables.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        children <span style="color: #339933;">=</span> tables<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">getElementsByTagName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'tr'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        Editable.<span style="color: #660066;">setChildrenLinkBehavior</span><span style="color: #009900;">&#40;</span>children<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
Editable.<span style="color: #660066;">setChildrenLinkBehavior</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>children<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> children.<span style="color: #660066;">length</span><span style="color: #339933;">;</span> i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onmouseover</span> <span style="color: #339933;">=</span> Editable.<span style="color: #660066;">showUtilLinks</span><span style="color: #339933;">;</span>
        children<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">onmouseout</span> <span style="color: #339933;">=</span> Editable.<span style="color: #660066;">hideUtilLinks</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Queues functions to load during window.onload event</span>
<span style="color: #003366; font-weight: bold;">function</span> addLoadEvent<span style="color: #009900;">&#40;</span>func<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> oldonload <span style="color: #339933;">=</span> window.<span style="color: #000066;">onload</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">typeof</span> window.<span style="color: #000066;">onload</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">'function'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> func<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        window.<span style="color: #000066;">onload</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>oldonload<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                oldonload<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            func<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #006600; font-style: italic;">// Add interface initialization to window.onload event</span>
addLoadEvent<span style="color: #009900;">&#40;</span>Editable.<span style="color: #660066;">initInterface</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
addLoadEvent<span style="color: #009900;">&#40;</span>Editable.<span style="color: #660066;">initUtilLinkBehavior</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This also uses a custom function called <code>addLoadEvent()</code> that adds an event to <code>window.onload</code>. This tended to be problematic in my application because there often was a slight &#8220;flicker&#8221; when you first loaded the page, where you could see all of the Edit/Delete links for a split second before the JavaScript fired and hid them.</p>
<h2>Doing it with jQuery</h2>
<p>When a coworker demoed some of his jQuery work to me and some colleagues, I felt a little more comfortable giving jQuery a try. It does look a little cryptic, so having someone explain some of it does help. Hopefully I can help you overcome any fears that you may have a little with the rest of this post.</p>
<h3>Getting rid of the <code>window.onload</code> flicker problem</h3>
<p>So the first way that I benefited from jQuery was by not needing to rely on <code>window.onload</code>.</p>
<p>jQuery provides a function to the <code>document</code> object called <code><a href="http://docs.jquery.com/Events/ready">ready()</a></code>, which basically helps you to load events as soon as the page is ready for them. This eliminates that flicker problem that I described before.</p>
<p>Here&#8217;s the gist:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Put events that you want to load when the page first loads here</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>To reiterate, the <code>$()</code> function is used here to load the <code>document</code> object, which we then call the <code>ready()</code> function on. We pass it a custom function where we can define whatever we want.</p>
<h3>Adding behavior to elements</h3>
<p>Basically, you use jQuery&#8217;s infamous dollar-sign function, <code>$()</code>, to select element(s) on the page by element name (like <code>li</code>, <code>a</code>, <code>ul</code>, etc.) or any <abbr title="Cascading Style Sheets">CSS</abbr> selector, be it an <code>id</code> or <code>class</code>. Then you can chain any number of function calls to that to manipulate behavior.</p>
<p>So here&#8217;s what I&#8217;d do to add a class called <code>hide-with-javascript</code> to all elements with a class of <code>util-links</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.util-links&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invisible-with-javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h3>Putting it all together</h3>
<p>The most remarkable thing is how this script does the exact same thing that my script does above with so much less code. It&#8217;s like the ColdFusion of JavaScript.</p>
<p>Here is the complete replacement script:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Interface initialization</span>
$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Hide all util links</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.util-links&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invisible-with-javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// Add hover-show, hover-out-hide</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.editable tbody tr&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hover</span><span style="color: #009900;">&#40;</span>
        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.util-links&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invisible-with-javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;.util-links&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invisible-with-javascript&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The jQuery <code><a href="http://docs.jquery.com/Events/hover">hover()</a></code> event takes 2 parameters: what to do on hover and what to do when your mouse leaves the hover state. In this case, we pass it 2 quick user defined functions inline.</p>
<p>I was impressed at how quick I was able to pick this up. It took me about a half hour to learn the applicable parts of the <abbr title="Application Programming Interface">API</abbr> and to get it implemented.</p>
<p>I&#8217;m looking forward to learning more about jQuery as my project calls for it. I&#8217;m sure that the <acronym title="Asynchronous JavaScript and XML">AJAX</acronym>y stuff that it provides will be pretty nifty and fast.</p>

<!-- start wp-tags-to-technorati 1.01 -->

<p class='technorati-tags'>Technorati Tags: <a class='technorati-link' href='http://technorati.com/tag/ccm' rel='tag' target='_self'>ccm</a>, <a class='technorati-link' href='http://technorati.com/tag/chris+peters' rel='tag' target='_self'>chris peters</a>, <a class='technorati-link' href='http://technorati.com/tag/clear+crystal+media' rel='tag' target='_self'>clear crystal media</a>, <a class='technorati-link' href='http://technorati.com/tag/html' rel='tag' target='_self'>html</a>, <a class='technorati-link' href='http://technorati.com/tag/javascript' rel='tag' target='_self'>javascript</a>, <a class='technorati-link' href='http://technorati.com/tag/jQuery' rel='tag' target='_self'>jQuery</a>, <a class='technorati-link' href='http://technorati.com/tag/polymorphism' rel='tag' target='_self'>polymorphism</a>, <a class='technorati-link' href='http://technorati.com/tag/Programming' rel='tag' target='_self'>Programming</a>, <a class='technorati-link' href='http://technorati.com/tag/web+development' rel='tag' target='_self'>web development</a></p>

<!-- end wp-tags-to-technorati -->
]]></content:encoded>
			<wfw:commentRss>http://www.clearcrystalmedia.com/pm/my-first-usage-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
