<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>jQuery Minute™</title>
	
	<link>http://jqueryminute.com</link>
	<description>...a jQuery Minute™ later and you're done!</description>
	<lastBuildDate>Tue, 24 Nov 2009 20:55:34 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JqueryMinute" /><feedburner:info uri="jqueryminute" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>JqueryMinute</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item>
		<title>SwitchView Plugin Released</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/ur3tU-CSeWU/</link>
		<comments>http://jqueryminute.com/switchview-plugin-released/#comments</comments>
		<pubDate>Tue, 24 Nov 2009 18:56:11 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Plugin-Extension]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/?p=85</guid>
		<description><![CDATA[Greetings all!
I&#8217;m pleased to release a new plugin called switchView. The SwitchView jQuery plugin provides the ability to easily define groups of elements to show and hide in one fell swoop. Let&#8217;s take a look at how the plugin works!
Given the following HTML:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
&#60;p&#62;
    &#60;label&#62;Username&#60;/label&#62;
    &#60;input type=&#34;text&#34; name=&#34;username&#34; /&#62;
&#60;/p&#62;
&#160;
&#60;p&#62;
  [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings all!</p>
<p>I&#8217;m pleased to release a new plugin called switchView. The <a href="http://jdsharp.com/jquery-plugins/switchview/">SwitchView jQuery plugin</a> provides the ability to easily define groups of elements to show and hide in one fell swoop. Let&#8217;s take a look at how the plugin works!</p>
<p>Given the following HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;
    &lt;label&gt;Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;p&gt;
    &lt;label&gt;Password&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;password&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;p&gt;
    &lt;label&gt;Confirm Password&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;confirmPassword&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;a href=&quot;#&quot;&gt;Change Password&lt;/a&gt;
&nbsp;
&lt;input type=&quot;submit&quot; value=&quot;Login&quot; /&gt;
&lt;input type=&quot;cancel&quot; value=&quot;Cancel&quot; /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Change Password&quot; /&gt;</pre></td></tr></table></div>

<p>The requirement is to toggle between a set of elements for the &#8220;login&#8221; view and the &#8220;change password&#8221; view. When using the switchView plugin, you define a &#8220;view&#8221; simply by placing a CSS class prefixed with <code>view-on-</code> on an element (the prefix can be configured). So let&#8217;s define two views: <code>view-on-login</code> and <code>view-on-change-pw</code>. Then to switch to the change-pw view, do:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">switchView</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'changePw'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now let&#8217;s look at how we would add the CSS classes to our elements:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;
    &lt;label&gt;Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;p&gt;
    &lt;label&gt;Password&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;password&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;p class=&quot;view-on-change-pw view-hidden&quot;&gt;
    &lt;label&gt;Confirm Password&lt;/label&gt;
    &lt;input type=&quot;password&quot; name=&quot;confirmPassword&quot; /&gt;
&lt;/p&gt;
&nbsp;
&lt;a href=&quot;#&quot; class=&quot;view-on-login&quot;&gt;Change Password&lt;/a&gt;
&nbsp;
&lt;input type=&quot;submit&quot; value=&quot;Login&quot; class=&quot;view-on-login&quot; /&gt;
&lt;input type=&quot;cancel&quot; value=&quot;Cancel&quot; class=&quot;view-on-change-pw view-hidden&quot; /&gt;
&lt;input type=&quot;submit&quot; value=&quot;Change Password&quot; class=&quot;view-on-change-pw view-hidden&quot; /&gt;</pre></td></tr></table></div>

<p>You&#8217;ll notice that I didn&#8217;t add any classes to the Username or Password fields as these are used in both views. Another option would be to place two view classes on those such as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;p class=&quot;view-on-login view-on-change-pw&quot;&gt;
    &lt;label&gt;Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;
&lt;/p&gt;</pre></td></tr></table></div>

<p>Additionally the <code>view-hidden</code> class has a single rule of <code>.view-hidden { display: none; }</code> which is used to hide elements by default.</p>
<p>Two final points to wrap up here, the context and groups. In the code below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">switchView</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'changePw'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The switchView plugin is looking for any elements that contain a <code>view-on-*</code> class within the <code>BODY</code>. It&#8217;s possible to scope switchView to only work on say a div with ID <code>#navigation</code>.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#navigation'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">switchView</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'changePw'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The one thing to watch out for, is if you change the view based on the <code>BODY</code> element, it will then potentially hide the elements inside of #navigation which is most likely not what you want. So to address this, the switchView jQuery Plugin provides for the concept of a &#8220;group&#8221;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;div class=&quot;view-group-login&quot;&gt;
&lt;p&gt;
    &lt;label&gt;Username&lt;/label&gt;
    &lt;input type=&quot;text&quot; name=&quot;username&quot; /&gt;
&lt;/p&gt;
...
&lt;/div&gt;</pre></td></tr></table></div>

<p>Now that we have grouped our login form, we would do the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">switchView</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'login.changePw'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>You can still use ungrouped elements, but doing so will not affect the elements within the <code>view-group-login</code>. It&#8217;s also worth noting that you can have grouped elements located anywhere on the page. So for example, if you have two parts to your form that are in physically different locations in your document, you can just add a <code>view-group-login</code> to each section.</p>
<p><a href="http://jdsharp.com/jquery-plugins/switchview/">jQuery switchView Plugin by jdsharp</a></p>
<p>Enjoy!</p>
<p>Cheers,<br />
- Jonathan</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/ur3tU-CSeWU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/switchview-plugin-released/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/switchview-plugin-released/</feedburner:origLink></item>
		<item>
		<title>Duplicate and Clone Elements Multiple Times</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/NjWZeaZ6qiA/</link>
		<comments>http://jqueryminute.com/duplicate-and-clone-elements-multiple-times/#comments</comments>
		<pubDate>Mon, 09 Nov 2009 14:15:19 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[Plugin-Extension]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/?p=74</guid>
		<description><![CDATA[Greeting again from the heartland. Here is a quick plugin that duplicates one or more elements n times. This function is similar to the clone() method but will clone the elements multiple times placing them in the same collection. If you were to call clone 5 times such as:

1
$&#40;'&#60;div&#62;&#60;/div&#62;'&#41;.clone&#40;&#41;.clone&#40;&#41;.clone&#40;&#41;.clone&#40;&#41;.clone&#40;&#41;;

it would give you 5 separate collections [...]]]></description>
			<content:encoded><![CDATA[<p>Greeting again from the heartland. Here is a quick plugin that duplicates one or more elements n times. This function is similar to the clone() method but will clone the elements multiple times placing them in the same collection. If you were to call clone 5 times such as:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'&lt;div&gt;&lt;/div&gt;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>it would give you 5 separate collections of a div. The duplicate plugin below, puts a new collection of all elements duplicated on the jQuery stack.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;ul id=&quot;myList&quot;&gt;
    &lt;li&gt;&lt;a href=&quot;http://www.flickr.com/photos/jdsharp-com/4088227698/&quot;&gt;Percheron's are cool&lt;/a&gt;&lt;/li&gt; 
&lt;/ul&gt;</pre></td></tr></table></div>

<p>So here&#8217;s an example use case given the HTML above:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'li'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">duplicate</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">5</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#myList'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above code starts by selecting all LI on the page (in this case only 1 of them) then duplicate&#8217;s it 5 times and appends the duplicated element to the UL with an ID of myList.</p>
<p>Now for the plugin:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">duplicate</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>count<span style="color: #339933;">,</span> cloneEvents<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> tmp <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</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> i <span style="color: #339933;">&lt;</span> count<span style="color: #339933;">;</span> i<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		$.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span> tmp<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">clone</span><span style="color: #009900;">&#40;</span> cloneEvents <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pushStack</span><span style="color: #009900;">&#40;</span> tmp <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Having this plugin return <code>this.pushStack( tmp )</code> is what gives us a new collection. To pop this collection off the stack and return to our original elements call <code>.end()</code>. One final note, cloneEvents can be set to true to have jQuery&#8217;s clone method also clone any attached events. By default it clones just the element and not attached events.</p>
<p>That&#8217;s all for now!<br />
- Jonathan</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/NjWZeaZ6qiA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/duplicate-and-clone-elements-multiple-times/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/duplicate-and-clone-elements-multiple-times/</feedburner:origLink></item>
		<item>
		<title>Calculating The Sum of Inputs</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/QHeIPveGjsg/</link>
		<comments>http://jqueryminute.com/calculating-the-sum-of-inputs/#comments</comments>
		<pubDate>Fri, 06 Nov 2009 14:00:17 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[Plugin-Extension]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/?p=69</guid>
		<description><![CDATA[Greetings all, here&#8217;s a quick plugin to sum up the values of inputs or even html elements. I wrote this plugin recently for a fellow developer who had a series of input boxes that needed to be totaled. Let&#8217;s first take a look at the html:

1
2
3
4
5
6
Price 1: &#60;input type=&#34;text&#34; name=&#34;price01&#34; class=&#34;price&#34; /&#62;&#60;br/&#62;
Price 2: &#60;input type=&#34;text&#34; [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings all, here&#8217;s a quick plugin to sum up the values of inputs or even html elements. I wrote this plugin recently for a fellow developer who had a series of input boxes that needed to be totaled. Let&#8217;s first take a look at the html:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">Price 1: &lt;input type=&quot;text&quot; name=&quot;price01&quot; class=&quot;price&quot; /&gt;&lt;br/&gt;
Price 2: &lt;input type=&quot;text&quot; name=&quot;price02&quot; class=&quot;price&quot; /&gt;&lt;br/&gt;
Price 3: &lt;input type=&quot;text&quot; name=&quot;price03&quot; class=&quot;price&quot; /&gt;&lt;br/&gt;
Price 4: &lt;input type=&quot;text&quot; name=&quot;price04&quot; class=&quot;price&quot; /&gt;&lt;br/&gt;
Price 5: &lt;input type=&quot;text&quot; name=&quot;price05&quot; class=&quot;price&quot; /&gt;&lt;br/&gt;
Total: &lt;span class=&quot;total&quot;&gt;&lt;/span&gt;</pre></td></tr></table></div>

<p>The HTML above allows for entering a price for each of five items. (price01 through price05). While it is possible to manually select each element and sum their totals, in the actual use case there are a number of additional inputs.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">sumValues</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> sum <span style="color: #339933;">=</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">;</span> 
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</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: #000066; font-weight: bold;">is</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">':input'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> val <span style="color: #339933;">=</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;">val</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: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #003366; font-weight: bold;">var</span> val <span style="color: #339933;">=</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;">text</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		sum <span style="color: #339933;">+=</span> parseFloat<span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'0'</span> <span style="color: #339933;">+</span> val<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/[^0-9-\.]/g</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">10</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: #000066; font-weight: bold;">return</span> sum<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now the actual plugin starts by iterating over each of the inputs that the user selected (in our case the selector is <code>input.price</code>) and keeps a running tally. Line 4 determines of the current element selected is an input element, otherwise it uses the .text() jQuery method to return the contents of the element minus any HTML tags. Line 9 strips out any non-digit characters, &#8211; and . and parses the string into a float. <code>('0' + val)</code> portion ensures that we have at least a value in the field. Finally the second argument to <code>parseFloat()</code> ensures that the float is parsed in base 10 for the conditions where the user entered a leading zero.</p>
<p>Finally, let&#8217;s make use of our plugin and wire it up:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td 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;">'input.price'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'keyup'</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: #3366CC;">'span.total'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">html</span><span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'input.price'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">sumValues</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</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></td></tr></table></div>

<p>On line 2 we bind the keyup event to all inputs with a class of price, sum up the values of those inputs and assign the result to <code>span.total</code>. There you go!</p>
<p>Until next time,<br />
- Jonathan</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/QHeIPveGjsg" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/calculating-the-sum-of-inputs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/calculating-the-sum-of-inputs/</feedburner:origLink></item>
		<item>
		<title>jQuery add or remove class with toggleClass</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/dd6mLtTe2bs/</link>
		<comments>http://jqueryminute.com/jquery-add-or-remove-class-with-toggleclass/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 05:13:43 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[jQuery 101]]></category>
		<category><![CDATA[jQuery Core]]></category>
		<category><![CDATA[jQuery Documentation]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/?p=44</guid>
		<description><![CDATA[When you need to explicitly add or remove a class based upon a separate condition, toggleClass is the way to go. The second attribute allows for explicitly specifying if a class should be added or removed.]]></description>
			<content:encoded><![CDATA[<p>Back in May of 2008 I posted about how to <a href="/jquery-as-an-associative-array-dynamically-calling-jquery-functions">dynamically call a jQuery function</a> and the practical use in adding or removing a class in one fell swoop. Since that post the <code>toggleClass</code> method has been updated to accept a second argument, a boolean value to signify adding or removing a class.</p>
<p>First the classic toggle class example:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Which just as the method describes will add the class &#8220;blue&#8221; if it doesn&#8217;t exit or remove it if it does.</p>
<p>In the next example we make use of addClass or removeClass</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'makeBlue'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #009900;">&#41;</span><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>
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Which has the same result and function as our first example with the difference that we&#8217;re adding or removing a class based upon the condition of <code>body</code> having the class &#8220;makeBlue&#8221;.</p>
<p>So here is an updated example making use of <code>toggleClass</code> along with the second argument that when true adds the class and conversely if false removes the class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> addBlue <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'makeBlue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #339933;">,</span> addBlue <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And the final reduced version:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toggleClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'blue'</span><span style="color: #339933;">,</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'makeBlue'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Cheers,<br />
- Jonathan</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/dd6mLtTe2bs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/jquery-add-or-remove-class-with-toggleclass/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/jquery-add-or-remove-class-with-toggleclass/</feedburner:origLink></item>
		<item>
		<title>Finding Immediately Adjacent Siblings</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/-8rdlR3iXes/</link>
		<comments>http://jqueryminute.com/finding-immediately-adjacent-siblings/#comments</comments>
		<pubDate>Tue, 15 Jul 2008 02:34:34 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[Plugin-Extension]]></category>
		<category><![CDATA[jQuery 101]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/finding-immediately-adjacent-siblings/</guid>
		<description><![CDATA[Greetings! Here&#8217;s a quick tip for advanced filtering of adjacent sibling nodes.
Given the following HTML:

1
2
3
4
5
6
7
8
&#60;ul&#62;
    &#60;li&#62;Item 1&#60;/li&#62;
    &#60;li class=”hello”&#62;Item 2&#60;/li&#62;
    &#60;li class=”hello” id=”three”&#62;Item 3&#60;/li&#62;
    &#60;li class=”hello”&#62;Item 4&#60;/li&#62;
    &#60;li&#62;Item 5&#60;/li&#62;
    &#60;li class=”hello”&#62;Item 6&#60;/li&#62;
&#60;/ul&#62;

If you needed to select the [...]]]></description>
			<content:encoded><![CDATA[<p>Greetings! Here&#8217;s a quick tip for advanced filtering of adjacent sibling nodes.</p>
<p>Given the following HTML:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;ul&gt;
    &lt;li&gt;Item 1&lt;/li&gt;
    &lt;li class=”hello”&gt;Item 2&lt;/li&gt;
    &lt;li class=”hello” id=”three”&gt;Item 3&lt;/li&gt;
    &lt;li class=”hello”&gt;Item 4&lt;/li&gt;
    &lt;li&gt;Item 5&lt;/li&gt;
    &lt;li class=”hello”&gt;Item 6&lt;/li&gt;
&lt;/ul&gt;</pre></td></tr></table></div>

<p>If you needed to select the <em>immediate</em> siblings of #three that matched the class .hello (so the resulting element set would be Item 2, Item 3, Item 4) there isn&#8217;t an easy way to do this. jQuery provides a prev() and prevAll() methods (as well as their next() counter parts) but that would return all of the elements. Here&#8217;s a jQuery immediateSiblings function to take care of this for us:</p>
<p>$(&#8216;#three&#8217;).immediateSiblings(&#8216;li.hello&#8217;)</p>
<p>Here&#8217;s the code for the plugin:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">/*
 * immediateSiblings 1.1.0 (2009-10-19)
 *
 * Copyright (c) 2006-2009 Jonathan Sharp (http://jdsharp.com)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * http://jdsharp.com/
 *
 * Built upon jQuery 1.3.2 (http://jquery.com)
 */</span>
$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">immediateSiblings</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> siblings <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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: #003366; font-weight: bold;">var</span> children <span style="color: #339933;">=</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;">parent</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">children</span><span style="color: #009900;">&#40;</span>selector<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">not</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;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $.<span style="color: #660066;">merge</span><span style="color: #009900;">&#40;</span>siblings<span style="color: #339933;">,</span> children<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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">pushStack</span><span style="color: #009900;">&#40;</span> $.<span style="color: #660066;">unique</span><span style="color: #009900;">&#40;</span> siblings <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Cheers,<br />
-Jonathan</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/-8rdlR3iXes" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/finding-immediately-adjacent-siblings/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/finding-immediately-adjacent-siblings/</feedburner:origLink></item>
		<item>
		<title>Set Focus to the Next Input Field with jQuery</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/uF-T7UMbIuU/</link>
		<comments>http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/#comments</comments>
		<pubDate>Tue, 27 May 2008 22:02:33 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Plugin-Extension]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/set-focus-to-the-next-input-field-with-jquery/</guid>
		<description><![CDATA[Setting focus to input fields is easy enough with JavaScript: document.getElementById(&#8216;theInputField&#8217;).focus(); but sometimes you need a more generic solution as what happens when the next input field changes ID?
I was recently faced with the problem of setting focus to the next input field. The challenge was that I didn&#8217;t know what that field was. So [...]]]></description>
			<content:encoded><![CDATA[<p>Setting focus to input fields is easy enough with JavaScript: document.getElementById(&#8216;theInputField&#8217;).focus(); but sometimes you need a more generic solution as what happens when the next input field changes ID?</p>
<p>I was recently faced with the problem of setting focus to the next input field. The challenge was that I didn&#8217;t know what that field was. So given an input field, find the next logical (in the order of the DOM) input field and set focus. I came up with the following jQuery function (plugin) to accomplish this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">focusNextInputField</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;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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: #003366; font-weight: bold;">var</span> fields <span style="color: #339933;">=</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;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form:eq(0),body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button,input,textarea,select'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> fields.<span style="color: #660066;">index</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: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> index <span style="color: #339933;">&gt;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span> index <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> fields.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            fields.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span> index <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</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: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</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: #339933;">;</span></pre></td></tr></table></div>

<p>The use is as follows:</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;">'current_field_selector'</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">focusNextInputField</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Let&#8217;s break this code down some:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">fn</span>.<span style="color: #660066;">focusNextInputField</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></pre></div></div>

<p>Start off by adding a new jQuery function/plugin</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">each</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></pre></div></div>

<p>Given a set of elements (this =&gt; jQuery object), iterate over them (we&#8217;ll return false which stop iteration after the first element.)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> fields <span style="color: #339933;">=</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;">parents</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'form:eq(0),body'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'button,input,textarea,select'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Walk up the DOM tree (parents) until we find either the first form element or reach the body tag. Now find all button,input,textarea and select elements.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> index <span style="color: #339933;">=</span> fields.<span style="color: #660066;">index</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: #339933;">;</span></pre></div></div>

<p>Find out if our current DOM element (this) is in the jQuery collection. Index will return -1 if it is not.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> index <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #339933;">-</span><span style="color: #CC0000;">1</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #009900;">&#40;</span> index <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> fields.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>See if we have a match and make sure we aren&#8217;t the last element</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">fields.<span style="color: #660066;">eq</span><span style="color: #009900;">&#40;</span> index <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</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></pre></div></div>

<p>Select the next input field ( index + 1 ) and set focus to it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#125;</span>
<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span></pre></div></div>

<p>Return false so we stop iteration after this element. So if you call $(&#8230;).focusNextInputField() with multiple elements, it will only set focus to the next input field of the first element.</p>
<p>Good luck &amp; enjoy!</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/uF-T7UMbIuU" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/set-focus-to-the-next-input-field-with-jquery/</feedburner:origLink></item>
		<item>
		<title>jQuery as an Associative Array – Dynamically calling jQuery functions</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/kP6rUJfXnL0/</link>
		<comments>http://jqueryminute.com/jquery-as-an-associative-array-dynamically-calling-jquery-functions/#comments</comments>
		<pubDate>Thu, 15 May 2008 20:37:53 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[jQuery 101]]></category>
		<category><![CDATA[jQuery Core]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/jquery-as-an-associative-array-dynamically-calling-jquery-functions/</guid>
		<description><![CDATA[
This post has been updated

So how many times have you done the following:

1
2
3
4
5
6
var elm = $&#40;'.selector'&#41;;
if &#40; elm.hasClass&#40;'selectedElement'&#41; &#41; &#123;
    elm.removeClass&#40;'abc'&#41;;
&#125; else &#123;
    elm.addClass&#40;'abc'&#41;;
&#125;

One alternative to the above example is to use jQuery&#8217;s toggleClass() method, but toggleClass does exactly that, toggles the class without a way for us to [...]]]></description>
			<content:encoded><![CDATA[<div class="updated">
<a href="/jquery-add-or-remove-class-with-toggleclass/">This post has been updated</a>
</div>
<p>So how many times have you done the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elm <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.selector'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> elm.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selectedElement'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    elm.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><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>
    elm.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>One alternative to the above example is to use jQuery&#8217;s <a href="http://docs.jquery.com/Attributes/toggleClass">toggleClass()</a> method, but toggleClass does exactly that, toggles the class without a way for us to know the current state.</p>
<p>So now to the point of this post, accessing a jQuery function using associative array notation. Here&#8217;s an example of a typical jQuery operation:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Call addClass('abc') on all divs</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here&#8217;s the same jQuery operation using associative array notation.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Call addClass('abc') on all divs using associative array notation</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'div'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'addClass'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now the refactored example from the beginning of this post:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> elm <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'.selector'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
elm<span style="color: #009900;">&#91;</span> <span style="color: #009900;">&#40;</span> elm.<span style="color: #660066;">hasClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'selectedElement'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">?</span> <span style="color: #3366CC;">'remove'</span> <span style="color: #339933;">:</span> <span style="color: #3366CC;">'add'</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'Class'</span> <span style="color: #009900;">&#93;</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'abc'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Here&#8217;s some additional links on JavaScript objects as Associative Arrays:</p>
<ul>
<li><a href="http://www.quirksmode.org/js/associative.html">http://www.quirksmode.org/js/associative.html</a></li>
<li><a href="http://www.google.com/search?hl=en&amp;q=javascript+objects+as+arrays&amp;btnG=Search">Search Google for &#8220;javascript as associative arrays&#8221;</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/kP6rUJfXnL0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/jquery-as-an-associative-array-dynamically-calling-jquery-functions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/jquery-as-an-associative-array-dynamically-calling-jquery-functions/</feedburner:origLink></item>
		<item>
		<title>jQuery 1.2.3 Released (Quietly!)</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/R4n4r2F7AvA/</link>
		<comments>http://jqueryminute.com/jquery-123-released-quietly/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 19:41:33 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[jQuery Blogs & Sites]]></category>
		<category><![CDATA[jQuery Core]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/jquery-123-released-quietly/</guid>
		<description><![CDATA[I saw on this as part of the jQuery UI post on Ajaxian today, jQuery 1.2.3 was quietly released last night.
jQuery UI Google Groups Post
Here you go:
http://code.jquery.com/jquery-1.2.3.js
http://code.jquery.com/jquery-1.2.3.min.js
http://code.jquery.com/jquery-1.2.3.pack.js
]]></description>
			<content:encoded><![CDATA[<p>I saw on this as part of the <a href="http://ajaxian.com/archives/jquery-ui-and-jquery-enchant-alpha-versions-released-today">jQuery UI post on Ajaxian</a> today, jQuery 1.2.3 was quietly released last night.</p>
<p><a href="http://groups.google.com/group/jquery-ui/browse_thread/thread/db8276574e20a5be/6a406868c7390d83#6a406868c7390d83">jQuery UI Google Groups Post</a></p>
<p>Here you go:</p>
<p><a href="http://code.jquery.com/jquery-1.2.3.js">http://code.jquery.com/jquery-1.2.3.js</a><br />
<a href="http://code.jquery.com/jquery-1.2.3.min.js">http://code.jquery.com/jquery-1.2.3.min.js</a><br />
<a href="http://code.jquery.com/jquery-1.2.3.pack.js">http://code.jquery.com/jquery-1.2.3.pack.js</a></p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/R4n4r2F7AvA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/jquery-123-released-quietly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/jquery-123-released-quietly/</feedburner:origLink></item>
		<item>
		<title>jQuery Iteration and each()</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/CvwJzdklxfA/</link>
		<comments>http://jqueryminute.com/jquery-iteration-and-each/#comments</comments>
		<pubDate>Thu, 07 Feb 2008 14:00:44 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[Uncommon Features]]></category>
		<category><![CDATA[jQuery 101]]></category>
		<category><![CDATA[jQuery Core]]></category>
		<category><![CDATA[jQuery Documentation]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/jquery-iteration-and-each/</guid>
		<description><![CDATA[There was a recent thread on the jQuery discussion group in that there was a need for looping through a series of elements and dynamically binding an event. An issue was brought up with closures and the arguments for dynamic binding of functions. Brandon Aaron provided an elegant solution that makes use of jQuery&#8217;s internal [...]]]></description>
			<content:encoded><![CDATA[<p>There was a recent <a href="http://groups.google.com/group/jquery-en/browse_thread/thread/6b7efaf1db5a4255/730d820e0c020635?lnk=gst&amp;q=fighting+closure#730d820e0c020635" target="_blank">thread</a> on the jQuery discussion group in that there was a need for looping through a series of elements and dynamically binding an event. An issue was brought up with closures and the arguments for dynamic binding of functions. <a href="http://blog.brandonaaron.net/" target="_blank">Brandon Aaron</a> provided an elegant solution that makes use of jQuery&#8217;s internal each() function. Let&#8217;s jump in!</p>
<p>So here&#8217;s a paraphrase of Brandon&#8217;s code:</p>
<p><strong>Example 1:</strong><br />
jQuery.each( [0,1,2,3,4], function(index, item){<br />
// Your code<br />
});</p>
<p>At first glance this may look the same as your standard iteration code using each but it differs slightly and that is where the benefit is. Let&#8217;s look at using the standard each method on a jQuery object:</p>
<p><strong>Example 2:</strong><br />
$(&#8217;selector&#8217;).each(function(index){<br />
// Your code<br />
});</p>
<p>In this example jQuery internally manages the collection (array) of matched elements by the selector.</p>
<p>But, by using jQuery&#8217;s internal .each() method (Example 1) we&#8217;re able to pass in an arbitrary array in which each item will have the callback executed. An elegant solution to your typical for ( i ) loop. Your callback can accept two arguments: index, item where index is the numerical zero based index in the array of the current item and item is the value of the current array. Also worth noting is that by returning false from you callback you can stop iteration over the array (this is the same as using a break; statement in a for loop).</p>
<p>Happy coding!</p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/CvwJzdklxfA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/jquery-iteration-and-each/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/jquery-iteration-and-each/</feedburner:origLink></item>
		<item>
		<title>JavaScript Pretty Date by John Resig</title>
		<link>http://feedproxy.google.com/~r/JqueryMinute/~3/32W-3xGE9nk/</link>
		<comments>http://jqueryminute.com/javascript-pretty-date-by-john-resig/#comments</comments>
		<pubDate>Thu, 31 Jan 2008 16:30:48 +0000</pubDate>
		<dc:creator>jdsharp</dc:creator>
				<category><![CDATA[Code-bits]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://jqueryminute.com/blog/javascript-pretty-date-by-john-resig/</guid>
		<description><![CDATA[Once again the JavaScript master is at it with &#8220;Pretty Date&#8221; to take the unfriendly &#8220;2008-01-28T20:24:17Z&#8221; and turn it into: &#8220;2 hours ago&#8220;. Worth a look as it is short and sweet!
http://ejohn.org/blog/javascript-pretty-date/
]]></description>
			<content:encoded><![CDATA[<p>Once again the JavaScript master is at it with &#8220;Pretty Date&#8221; to take the unfriendly &#8220;<span style="color: #3366cc">2008-01-28T20:24:17Z</span>&#8221; and turn it into: &#8220;<span style="color: #009900; font-style: italic">2 hours ago</span>&#8220;. Worth a look as it is short and sweet!</p>
<p><a href="http://ejohn.org/blog/javascript-pretty-date/" target="_blank">http://ejohn.org/blog/javascript-pretty-date/</a></p>
<img src="http://feeds.feedburner.com/~r/JqueryMinute/~4/32W-3xGE9nk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://jqueryminute.com/javascript-pretty-date-by-john-resig/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://jqueryminute.com/javascript-pretty-date-by-john-resig/</feedburner:origLink></item>
	</channel>
</rss>
