<?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>MyInkBlog &#187; Javascript</title>
	<atom:link href="http://www.myinkblog.com/tutorials/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.myinkblog.com</link>
	<description>A Resource For All Things Design</description>
	<lastBuildDate>Sat, 10 Dec 2011 08:50:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Learn How To Create Your Own jQuery Plugin</title>
		<link>http://www.myinkblog.com/learn-how-to-create-your-own-jquery-plugin/</link>
		<comments>http://www.myinkblog.com/learn-how-to-create-your-own-jquery-plugin/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 02:02:23 +0000</pubDate>
		<dc:creator>Giulio Bai</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Article]]></category>
		<category><![CDATA[Plugin]]></category>

		<guid isPermaLink="false">http://www.myinkblog.com/?p=5905</guid>
		<description><![CDATA[<a href="http://jquery.com/" class="ext">JQuery</a> offers a very practical and easy way to build plugins, as well as a <a href="http://docs.jquery.com/Plugins/Authoring" class="ext">very good documentation</a> on this subject. However, it could be useful to see how a plugin is written step-by-step.]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.myinkblog.com/wp-content/uploads/2009/08/jquery-plugin1.jpg" alt="jquery-plugin" title="jquery-plugin" width="575" height="222" class="paddedborder" /></p>
<h2>Overview</h2>
<p><a href="http://jquery.com/" class="ext">JQuery</a> offers a very practical and easy way to build plugins, as well as a <a href="http://docs.jquery.com/Plugins/Authoring" class="ext">very good documentation</a> on this subject. However, it could be useful to see how a plugin is written step-by-step.</p>
<h2>Basic Structure</h2>
<p>The basis of creating a plugin for jQuery is actually quite simple. You don&#8217;t have to be a jQuery ninja, nor do you have to sweat blood to make it work. It&#8217;s as easy as script writing!</p>
<p>Following the jQuery plugin authoring guidelines is a good idea. Bear in mind that 1) plugins files should be named jquery.PLUGINNAME.js and 2) always attach your plugins to jQuery instead of $, so aliases can be easily set using the noConflict() method.</p>
<p>The structure required to extend the jQuery.fn object is&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">firstPlugin</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: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">id</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></pre></div></div>

<p>You&#8217;d now be able to call your plugin as you normally would call any other plugin</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;">'#test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">firstPlugin</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This would display a &#8220;nice&#8221; alert box showing the element id.</p>
<h2>One Step Further: Parameters</h2>
<p>Ok, I agree the above example was a bit lame, so let&#8217;s find out how a more complex plugin can be developed. You can code your plugin to require some parameters and accept other options as well. If your plugin needs two numbers, you could write it in the following way:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">secondPlugin</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>number1<span style="color: #339933;">,</span> number2<span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
myoptions <span style="color: #339933;">=</span> jQuery.<span style="color: #660066;">extend</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
operation<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sum&quot;</span><span style="color: #339933;">,</span>
label<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;The result is&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> options<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$<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;">html</span> <span style="color: #009900;">&#40;</span>myoptions.<span style="color: #660066;">label</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; (&quot;</span> <span style="color: #339933;">+</span> myoptions.<span style="color: #660066;">operation</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;)&quot;</span> <span style="color: #339933;">+</span> myoptions.<span style="color: #660066;">number1</span><span style="color: #339933;">+</span>myoptions.<span style="color: #660066;">number2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In the above code, check out the way we provide default settings in case the two strings are not passed to the plugin function. This way we can call either</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;">'#test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">secondPlugin</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>to get</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test The result is (sum) 3&lt;/span&gt;</span></span></pre></div></div>

<p>or</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;">'#test'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">secondPlugin</span> <span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span> operation<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sums two numbers together&quot;</span><span style="color: #339933;">,</span> label<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;We got&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>if you prefer</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">span</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;test We got (sums two numbers together) 3&lt;/span&gt;</span></span></pre></div></div>

<h2>The jQuery Object: Custom Functions and Selectors</h2>
<p>Until now, we&#8217;ve messed with the jQuery.fn object, which is responsible for elements interaction, and we never mentioned the jQuery object itself, which handles internal processing. By extending it, we are thus allowed to create our own functions and even selectors for others to use! Here&#8217;s how new methods can be added:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">fn</span>.<span style="color: #660066;">extend</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
myFirstFunction <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;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;first function&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
thisIsTheSecond <span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;2nd: &quot;</span><span style="color: #339933;">+</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>and then called without any problems</p>

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

<p>In a very similar fashion you can create your own selectors, provided they don&#8217;t exist yet.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;:&quot;</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">startsWith</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>element<span style="color: #339933;">,</span> index<span style="color: #339933;">,</span> tokens<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: #339933;">!</span>tokens<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</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: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">eval</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;/^[/s]*&quot;</span> <span style="color: #339933;">+</span> tokens<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/i&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">test</span> <span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span>element<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As you can see, the function takes three arguments, being: 1) the matched element, 2) the index of the element and 3) an object that contains parse tokens; the first element of the array is the full filter string (including parameters), the second one is the &#8220;:&#8221;, the third one is the filter name and, finally, the fourth is the parameter.</p>
<p>You should always check whether the parameter has been passed or not, since the filter could stop working in the latter case.</p>
<h2>Wrapping It Up</h2>
<p>These are some tips to get you started writing your own jQuery plugins. It&#8217;s only by diving in that you&#8217;ll fully learn all the things you can do.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.myinkblog.com/learn-how-to-create-your-own-jquery-plugin/feed/</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk: basic
Page Caching using disk: enhanced (User agent is rejected)
Database Caching 1/14 queries in 0.012 seconds using disk: basic
Object Caching 576/589 objects using disk: basic

Served from: www.myinkblog.com @ 2012-02-11 14:06:00 -->
