<?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>AddyOsmani.com &#124; Where User Interface Ideas Grows &#187; improve jquery</title>
	<atom:link href="http://addyosmani.com/blog/tag/improve-jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://addyosmani.com/blog</link>
	<description>This is the home of Addy Osmani (Web Developer, Designer &#38; Author). Here you can find some great tips and tutorials on everything to do with web development and even a few useful code samples!</description>
	<lastBuildDate>Fri, 03 Sep 2010 21:35:46 +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>7 Really Useful Tips For Better jQuery Code</title>
		<link>http://addyosmani.com/blog/7-really-useful-tips-for-better-jquery-code/</link>
		<comments>http://addyosmani.com/blog/7-really-useful-tips-for-better-jquery-code/#comments</comments>
		<pubDate>Mon, 21 Sep 2009 23:22:32 +0000</pubDate>
		<dc:creator>Addy</dc:creator>
				<category><![CDATA[Resources]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[better javascript]]></category>
		<category><![CDATA[better jquery]]></category>
		<category><![CDATA[better web applications]]></category>
		<category><![CDATA[improve DOM]]></category>
		<category><![CDATA[improve javascript]]></category>
		<category><![CDATA[improve jquery]]></category>
		<category><![CDATA[javascript tips]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery tips]]></category>

		<guid isPermaLink="false">http://addyosmani.com/blog/5-really-useful-tips-for-better-jquery-code/</guid>
		<description><![CDATA[&#160; I&#8217;ve been using jQuery in my web apps for over three years now and a lot of the time &#8211; I use it in projects for my clients too. It&#8217;s an incredibly versatile way of using JavaScript and today I thought I&#8217;d share some tips that helped me do things a little better. &#160; [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;">&nbsp;<img width="450" height="170" alt="" src="http://addyosmani.com/blog/wp-content/uploads/23.jpg" /></p>
<p>I&rsquo;ve been using jQuery in my web apps for over three years now and a lot of the time &ndash; I use it in projects for my clients too. It&rsquo;s an incredibly versatile way of using JavaScript and today I thought I&rsquo;d share some tips that helped me do things a little better.</p>
<p>&nbsp;</p>
<p><span id="more-92"></span></p>
<h3>1: Use $(document).ready() or define your scripts before the closing &lt;/body&gt; tag.</h3>
<p>&nbsp;</p>
<p>Although you might be used to defining your JavaScript functions without $(document).ready, I would recommend using it &ndash; everything inside it loads up as soon as the DOM does and even before the rest of your page&rsquo;s contents have. It&rsquo;ll also allow you to attach events to any of the elements on your page and it won&rsquo;t interfere with their mark-up.</p>
<p>&nbsp;</p>
<p>An example: I&rsquo;ve come across lots of cases where you might want jQuery to hide some of the content in your page. If you&rsquo;re running your function before the document is ready, you increase your chances of it not being executed at all if the server&rsquo;s taking a while to load up the whole page. So..play it safe and use this method instead.</p>
<p>&nbsp;</p>
<p>Experienced jQuery users can also define their scripts before the last body tag to make sure they&rsquo;re executed as soon as they are loaded by the DOM.</p>
<p>&nbsp;</p>
<h3>2: Client side storage is getting more and more popular &ndash; do you use the data method instead of storing your data in the DOM?</h3>
<p>&nbsp;</p>
<p>I keep seeing people doing this in their plugins to store data</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h5>jQuery:</h5>
<pre>
$('selector').attr('alt', &lsquo;I am a string of data&rsquo;);
</pre>
<pre>
// and then they access this data back through
</pre>
<pre>
$('selector').attr('alt');</pre>
<pre>
&nbsp;</pre>
<p>So&hellip;why&rsquo;s this such a bad thing? Well..it&rsquo;s because &lsquo;alt&rsquo; wasn&rsquo;t created for this purpose and HTML isn&rsquo;t supposed to be used for storing your data.&nbsp; Instead&hellip; why not go for the data() method in jQuery. It was made to offer a way to store associated data with an element on the page.</p>
<p>&nbsp;</p>
<p>Here&rsquo;s an example of what I&rsquo;m talking about.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h5>jQuery:</h5>
<pre>

$('selector').data('albumName1', &lsquo;The Best of Phil Collins&rsquo;);

// then you can access this data back through

$('selector').data('albumName1');&nbsp;

&nbsp;
</pre>
<p>The overall effect of this is that you can store your data with meaningful names *and* you&rsquo;re able to store as much information you want on any element on the page.&nbsp; It&rsquo;s an awesome little tool and one which you might just end up relying on some day.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h3>3: Use jQuery&rsquo;s built-in custom selectors</h3>
<p>&nbsp;</p>
<p>I won&rsquo;t go into the reasons why using something like Sizzle.js is a good idea, but jQuery has lots of<a href="http://docs.jquery.com/Selectors"> selectors</a> that are beyond your run of the mill CSS selectors, so use them. Some that I find handy are:</p>
<p>&nbsp;</p>
<p><code>:input </code>&nbsp;</p>
<p>Example: This&rsquo;ll get you all the &ldquo;input&rdquo; elements on your page whether they&rsquo;re text areas, text boxes, select lists or anything else.</p>
<p>&nbsp;</p>
<p><code>[attribute=value] </code>&nbsp;</p>
<p>Example: If I want to find an input element with the name &ldquo;eMail&rdquo; I would use <code>input[name='eMail']</code></p>
<p>&nbsp;</p>
<p><code>:eq(index)</code>&nbsp;</p>
<p>Example: Btw..this is very very useful. To get the fifth div on a page use div<code>:eq(5)</code></p>
<p>&nbsp;</p>
<h3>4: If you manipulate the DOM a lot use live().</h3>
<p>&nbsp;</p>
<p>When frequently adding elements to your page, attaching events or even running functions try to use the <em>live()</em> method. That way, you can easily do things like:</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<h5>jQuery:</h5>
<pre>
  $(&quot;div.fork&quot;).<strong>live</strong>(&quot;click&quot;, function(){
      $(this).<a href="http://docs.jquery.com/Manipulation/after">after</a>(&quot;&lt;p&gt;Another paragraph!&lt;/p&gt;&quot;);
    });</pre>
<pre>
&nbsp;</pre>
<p>This way, anytime you add a new div to your page with the class &ldquo;fork&rdquo;, it&rsquo;ll attach that click event to it.</p>
<p>&nbsp;</p>
<pre>
&nbsp;</pre>
<h3>5: Use the jQuery Form plugin to submit files using Ajax</h3>
<p>&nbsp;</p>
<p>Try out this <a href="http://malsup.com/jquery/form/">jQuery form plugin</a> &ndash; it allows you to easily submit files to your server&nbsp; via Ajax. The plugin uses a neat trick to do with an iframe to submit the data, but it&rsquo;s really easy to use. Simply add an input type file and&nbsp; use <code>$(form).ajaxSubmit();</code></p>
<p>Thats it and you should be good to go.</p>
<p>&nbsp;</p>
<h3>6: Please..don&rsquo;t call the same selector again and again</h3>
<p>&nbsp;</p>
<p>There&rsquo;s no need to have jQuery find the element you&rsquo;re interested in more than once so why don&rsquo;t we do it more efficiently.</p>
<p>&nbsp;</p>
<h5>jQuery:</h5>
<pre>

//rather than this

$(&lsquo;div.hi&rsquo;).css(&lsquo;color&rsquo;, &lsquo;#ffffff&rsquo;);

$(&lsquo;div.hi&rsquo;).text(&lsquo;hello world&rsquo;);

$('div.hi&rsquo;).addClass(&lsquo;amazingclass&rsquo;);

//lets do this

var $q = $(&lsquo;div.hi&rsquo;);

$q.css(&lsquo;color&rsquo;, &lsquo;#ffffff&rsquo;);

$q.text(&lsquo;hello world&rsquo;);

$q.addClass(&lsquo;amazingclass&rsquo;);
</pre>
<p>&nbsp;</p>
<h3>7: How to effectively use &ldquo;classes&rdquo; as a sort of flag</h3>
<p>&nbsp;</p>
<p>So, you might not be trying to store data, but do want to set a flag on a particular type of element that uses a class &ndash; for example, if you wanted to lock down a form after a user has switched from the &ldquo;edit&rdquo; mode on your page, jQuery provides you with the addClass method which can later be checked using the very useful <a href="http://docs.jquery.com/Traversing/hasClass">hasClass method</a></p>
]]></content:encoded>
			<wfw:commentRss>http://addyosmani.com/blog/7-really-useful-tips-for-better-jquery-code/feed/</wfw:commentRss>
		<slash:comments>46</slash:comments>
		</item>
	</channel>
</rss>
