<?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; jquery tips</title>
	<atom:link href="http://addyosmani.com/blog/tag/jquery-tips/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>jQuery Vs. JavaScript &#8211; The Masters Share Their Views On Vanilla JS</title>
		<link>http://addyosmani.com/blog/javascriptuncovered/</link>
		<comments>http://addyosmani.com/blog/javascriptuncovered/#comments</comments>
		<pubDate>Tue, 25 May 2010 00:03:05 +0000</pubDate>
		<dc:creator>Addy</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[chris coyier]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[david walsh]]></category>
		<category><![CDATA[debate]]></category>
		<category><![CDATA[james padolsey]]></category>
		<category><![CDATA[janko]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript tutorial]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery team]]></category>
		<category><![CDATA[jquery tips]]></category>
		<category><![CDATA[jquery tricks]]></category>
		<category><![CDATA[jquery vs javascript]]></category>
		<category><![CDATA[marco kupier]]></category>
		<category><![CDATA[mootools]]></category>
		<category><![CDATA[should i learn javascript]]></category>
		<category><![CDATA[soh tanaka]]></category>

		<guid isPermaLink="false">http://addyosmani.com/blog/?p=724</guid>
		<description><![CDATA[&#160; We&#8217;ve got a really great post for you today guys. We&#8217;re going to explore whether it&#8217;s fundamentally important for designers and developers to understand how to use vanilla JavaScript and not just it&#8217;s derivative frameworks and libraries such as jQuery and MooTools. You might be asking &#8216;why?&#8217;, but read on and you&#8217;ll find out!. [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://addyosmani.com/blog/javascriptuncovered/"> <img border="0" alt="" src="http://addyosmani.com/blog/wp-content/uploads/masters.jpg" style="border: 0px none; display: inline;" /> </a></p>
<p>&nbsp;</p>
<p>We&#8217;ve got a really great post for you today guys. We&#8217;re going to explore whether it&#8217;s fundamentally important for designers and developers to understand how to use vanilla JavaScript and not just it&#8217;s derivative frameworks and libraries such as jQuery and MooTools. You might be asking &#8216;why?&#8217;, but read on and you&#8217;ll find out!. Because this is a topic that can invoke passion on both sides of the debate, I&#8217;ve asked a few familiar names to join the discussion including David Walsh, Chris Coyier, Marco Kupier, Janko Javanovic and James Padolsey. All of these authors use jQuery regularly and are in a good position to advise on the pros and cons of knowing vanilla JS.<br />
<span id="more-724"></span> Before we begin, I&#8217;d like to take this opportunity to introduce James Jackson as a new member of the site team who&#8217;s contribution to this post was invaluable. So let&#8217;s get started!.</p>
<p>We all know that JavaScript frameworks have made it significantly easier to create rich animated effects and interfaces in our web pages &#8211; the type of thing which previously would have taken a lot longer if we were writing everything from scratch. I&#8217;ve seen coders creating sliding effects and javascript animation for over 10 years but until now, the effort required to achieve the effects through plain JS were sometimes considered too much hassle. Throw in cross-browser compatibility issues and you had a nightmare. jQuery, MooTools and other frameworks have changed this.</p>
<p>They haven&#8217;t altered the underlying javascript (it&#8217;s important to point this out), but they have introduced an easier layer of access to JS and it&#8217;s core functionality which was previously missing. </p>
<p>The question we&#8217;re asking today is whether it&#8217;s important for people who use libraries like jQuery to understand how&nbsp; the underlying javascript works. My personal opinion is that it is &#8211; whilst you can &quot;get by&quot; knowing how to use many of the basic functins jQuery offers, I think that there&#8217;s a certain power in knowing why a particular selector should be used over another or how to create something from scratch if jQuery doesn&#8217;t particularly support executing it well.</p>
<p>Let&#8217;s now delve into this topic further with James.</p>
<p><span style="font-size: x-large;"><strong>jQuery Vs. JavaScript</strong></span></p>
<p>jQuery is the key to the Web 2.0 lock.</p>
<p>I&#8217;m not afraid to say it, and furthermore, I&#8217;m not afraid to utilize a well known buzz term like, &quot;Web 2.0&quot;.&nbsp; Now, throughout this article, I&#8217;m going to be referencing jQuery quite a bit but that&#8217;s only because it&#8217;s my favorite Javascript library and the one I know the best.&nbsp; You could easily support any number of other libraries in a similar fashion.</p>
<p>That said, jQuery has helped a whole new generation of coders approach that ever alluring, and always intimidating field of web scripting.&nbsp; Browsers themselves are greatly measured by how well (and quickly!) they can support Javascript, and jQuery alike.</p>
<p>Most people reading this article are likely already quite familiar with jQuery, so I&#8217;d like to take a generous step backward and talk about what lies beneath it.&nbsp; Javascript itself.</p>
<p>We all understand Javascript in different ways, possible by the vast variability of its usefulness, but at its core Javascript really showcases itself as a powerful, easy to use scripting language that is more accessible than many of its influences.&nbsp; The notorious C, and its derivitaves, is often understood as a pipe dream to many modern developers and yet every single Javascript programmer utilizes a syntax almost indisinguishable from C&#8217;s without ever realizing it.</p>
<p>jQuery further blurs the lines with its own, innovative syntax which causes its biggest strength to also be its biggest weakness: approachability.&nbsp; Within any system, its a common rule of thumb that the simpler it is to use, the more removed from the source material it gets.&nbsp; jQuery is no exception.</p>
<p>A great deal of jQuery programmers I have known since its inception have been drawn to jQuery for its simplicity, betwitched by its seeming ability to allow the average web designer to magically produce Javascript effects that were otherwise impossible.&nbsp; When a client wants something to zoom across the screen, or to showcase a slideshow of photographs, jQuery answers the call.&nbsp; With a few script includes and some copying and pasting, its not only possible but extremely effective.&nbsp; With the overwhelming amount of customization that jQuery Plugin developers put into their modules, coders can even have intricate control over these effects without ever touching a single line of raw Javascript.&nbsp; jQuery takes care of all of it for you.</p>
<p>The problem herein lies in the moment that every late-night script surgeon will inevitably encounter: when something doesn&#8217;t do *exactly* what we need it to.&nbsp; We try tweaking the settings, finding an alternative plugin or scaring ourselves witless looking at the wall of code within the plugins themselves.</p>
<p>Don&#8217;t worry &#8212; we&#8217;ve all been there.&nbsp; This frustration, however, doesn&#8217;t have to halt our programming process and can instead offer an exciting challenge.&nbsp; If we understand the underlying code well enough, then virtually nothing is outside of our grasp within the world of Javascript.</p>
<p>Virtually any element within a web page, through the most popular operating system on the planet &#8212; the web browser, is accessible and can have its attributes changed.&nbsp; New elements can be created, manipulated and even removed from existence.&nbsp; Your keyboard becomes your brush and the browser becomes your canvas.&nbsp; Literally, even now with HTML5.</p>
<p>jQuery provides a proverbial &#8216;Paint by Numbers&#8217; tool for us, but when we need to break away from the lines and numerical digits, the paper beneath needs to be understood as well.</p>
<p>I&#8217;m sure it goes without saying, but I&#8217;d bet that the average jQuery enthusiast without extensive knowledge of Javascript doesn&#8217;t fully realize what jQuery is: a collection of Javascript.&nbsp; It&#8217;s not a new environment, it&#8217;s not an expansion pack; it&#8217;s a big pile of Javascript that draws a curtain between you and Javascript.&nbsp; All the web&#8217;s a stage, and the DOM is its players.</p>
<p>Most readers, who are familiar with jQuery know of the $() identifier.&nbsp; It allows you to select, or create/remove, any element on a webpage and manipulate it.&nbsp; If we want to grab an element like this:</p>
<pre name="code" class="javascript">
<div id="example">&nbsp;</div>
</pre>
<p>Then all we require to select it is:</p>
<pre class="javascript" name="code">
$(&quot;#example&quot;)</pre>
<p>From there, we can affect it in the multitude of ways that jQuery provides.&nbsp; What does this $() mean, though, exactly?&nbsp; How does it work?&nbsp; Is it magic?</p>
<p>In all actuality, $()&#8217;s usefulness, speed and versatility is bound only by your understanding of it. Before we delve further into this, lets remember that $() is a jQuery selector which can take more than one parameter, element IDs separated by commas and returns elements as objects in an array. For the sake of simplicity, let&#8217;s imagine that we&#8217;re just trying to get the very first element matched in the DOM with a particular ID (in jQuery this translates to $(&#8216;#example&#8217;)[0]) In this case, we could achieve this in standard vanilla JavaScript as follows:</p>
<pre class="javascript" name="code">
document.getElementById(&quot;example&quot;)</pre>
<p>Now, because Javascript has this functionality included right into its core, calling elements in this way greatly speeds up jQuery (tests show that getElementById is faster than standard selection in jQuery, however, this is only when you want to grab elements of a particular ID rather than a collection of elements).&nbsp; Because we&#8217;re making it so that jQuery doesn&#8217;t have to churn through functions and translators, and instead can just use Javascript&#8217;s built-in functionality to jump right to the item. Again, let&#8217;s remember that jQuery actually provides us with ways of accessing elements by much more advanced selection techniques, however, if you know exactly what you&#8217;re after, vanilla can help.</p>
<p>What does this mean to you?&nbsp; SPEED.&nbsp;</p>
<p>By understanding this aspect of Javascript, you can literally cut whole chunks of processing time out of your plugins and scripts. We should note however that jQuery&#8217;s use of the Sizzle selector engine has done it&#8217;s best to eliminate redundancy when looking up elements in the DOM, but there are still gains to be made for those that understand the underlying javascript.</p>
<p>The Animate feature of jQuery, as mentioned above, allows us to move any element on a page around the browser&#8217;s view port with only a few lines of code:&nbsp;</p>
<pre name="code" class="javascript">
$(&quot;#element&quot;).animate({ left: 100px; });</pre>
<p>This makes moving #element 100 pixels to the right extremely simple.&nbsp; However, let&#8217;s assume that we want to add a wavy-animation to our element, so it doesn&#8217;t mearly glide to the left but instead waves around (as though our precious #element has had a few to many Box Model Martini&#8217;s).&nbsp; This is possible with jQuery, of course, and there are even plugins available to accomplish it.&nbsp; Let&#8217;s further assume that either we have a very specific vision for how it will animate or (more likely) our client does.</p>
<p>Either way, time will be spent.&nbsp; Whether its time spent looking for the appropriate plugin, or writing the code ourself.</p>
<p>If we briefly break down jQuery.Animate, we get a very simple process: an event occurring within an interval or timer, selecting the element (by ID) and changing its &#8216;style.left&#8217; property incrementally through CSS.&nbsp;. Animate() of course supports a wide range of features including intervals, easing and other plugins, but if we wanted to take animate at it&#8217;s most basic and wanted to re-create this in basic javascript (moving an element across the screen), lets see how we cold create that code.</p>
<pre class="javascript" name="code">
var example = null; // object

function doMove() {
  example.style.left = parseInt(example.style.left)+1+'px';
  setTimeout(doMove,20); // call doMove in 20msec
}

function init() {
  example = document.getElementById('example'); // get the &quot;example&quot; object
  example.style.left = '0px'; // set its initial position to 0px
  doMove(); // start animating
}

window.onload = init;
</pre>
<p>&nbsp;</p>
<p>And we&#8217;re done.&nbsp;Now obviously, there is a LOT more going on behind the animate function &#8211; it supports chaining, easing plugins and a greater level of modification than simply moving an item across the screen, but in its most basic form, that&#8217;s what we&#8217;re trying to achieve.</p>
<p>This is a more advanced example, so what if you want to mimic a much simpler function of jQuery?&nbsp; Say, the hide() command?</p>
<p>&nbsp;</p>
<pre name="code" class="javascript">
$(&quot;example&quot;).hide();</pre>
<p>&nbsp;</p>
<p>This comes in really handy, as you can hide elements that are not currently needed in your application.&nbsp; What does this function *do* exactly, though?&nbsp; As with so many things, the better we understand something, the better we can innovate.</p>
<p>Much of what makes Web Apps so functional (and, let&#8217;s admit it, pretty) is affecting their CSS attributes: their color, their opacity, their location on the screen, etc.&nbsp; The hide() function affects one of the most used CSS attributes: display.</p>
<p>To translate the above hide command, we could also imagine it like this:&nbsp;</p>
<p>&nbsp;</p>
<pre name="code" class="javascript">
 $(&quot;#example&quot;).css(&quot;display&quot;,&quot;none&quot;);</pre>
<p>&nbsp;</p>
<p>Of course, the hide() command gives you more control over the effect, but by using the css() command instead, we can now do some pretty interesting things, such as:</p>
<p>Blend an object in with the text, rather than putting it on its own line:</p>
<p>&nbsp;</p>
<pre name="code" class="javascript">
 $(&quot;#example&quot;).css(&quot;display&quot;,&quot;inline&quot;);</pre>
<p>&nbsp;</p>
<p>The opposite, take an inline element (e.g. &lt;strong&gt;) and make it take up a whole line:</p>
<p>&nbsp;</p>
<pre name="code" class="javascript">
 $(&quot;#example&quot;).css(&quot;display&quot;,&quot;block&quot;);</pre>
<p>&nbsp;</p>
<p>That&#8217;ll get someone&#8217;s attention!</p>
<p>Far too often, a jQuery developer will find themselves poking and prodding a bit of jQuery code, trying to get it to behave properly; when their answer is just a css() command away.</p>
<p>This is not to say that you have to choose between Javascript or jQuery, though.&nbsp; By combining the two, we can still use jQuery for what it&#8217;s best at: selecting elements and manipulating their attributes.&nbsp; For example, instead of using the whole line of document.getElementById(&quot;element&quot;) we can save ourselves some code by using $(&quot;#element&quot;).&nbsp; jQuery has a built-in checker for elements selected by that and will essentially translate it to our full &quot;getElementById&quot; line, anyhow, so we are literally just using it to abbreviate our code.</p>
<p>This is just the tip of the iceberg.&nbsp; Technically speaking, jQuery is a collection of snippets that allow someone to abide by the D.R.Y. (Don&#8217;t Repeat Yourself) method of programming.&nbsp; I urge you, however, to take advantage of jQuery, but not completely depend on it to code.</p>
<p>jQuery is a tool.&nbsp; Javascript is the garden.&nbsp; Together, they can allow us to cultivate endless abundance if utilized in harmony.</p>
<p>Knowing jQuery (among many other scripting libraries) in this modern world of web development is crucial.&nbsp; It provides quicker turn around times, opens your code up to a vast world of other developers and makes your code efficient, and easy to read.&nbsp; However, even the best tools are only as useful as the person who wields them.</p>
<p>If we have not only a better understanding of our tools, but also our environment, then we can help usher in &#8212; if not pioneer &#8212; the next era of web developers.</p>
<p>&nbsp;</p>
<p><span style="font-size: x-large;"><strong>Thoughts from:</strong></span><strong></p>
<p><span style="font-size: large;">David Walsh</span><br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</strong>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;</p>
<p><img width="200" vspace="10" hspace="10" height="200" align="left" style="padding: 10px;" alt="" src="http://addyosmani.com/blog/wp-content/uploads/david.jpg" /></p>
<p>&quot;Learning JavaScript is absolutely paramount to becoming an effective Web Designer or Developer.&nbsp; While the JavaScript libraries do as much as possible to ease the pain of some of JavaScript&#8217;s more time-consuming tasks, libraries cannot anticipate their every use.&nbsp; Without proper knowledge of JavaScript, a developer can spend hours debugging an issue that should really only take a few moments.&nbsp; Spending the time to become a &#8216;Vanilla JS&#8217; expert will allow a developer to more easily learn *any* of the popular frameworks, thus making that developer far more valuable.&quot;</p>
<p>&nbsp;</p>
<p><span style="font-size: large;"><strong>Chris Coyier</strong></span></p>
<p><img width="200" vspace="10" hspace="10" height="200" align="left" style="padding: 10px;" alt="" src="http://addyosmani.com/blog/wp-content/uploads/chris.jpg" /></p>
<p>&quot;I&#8217;d love to tell you all about how life will be better if you learn and understand JavaScript before you learn libraries. Or at least, that you should go back and get those fundamentals after starting out with libraries. Here is reality for me though: I&#8217;m terrible at vanilla JavaScript. It would be pretty hard for me to try and get day-to-day design work done if I couldn&#8217;t use jQuery. In fact, I probably just wouldn&#8217;t do it, and stick to design and markup. I&#8217;d love to go back and get those fundamentals, and I probably will over time, and it will make me better, but I&#8217;m also living proof that you can still get the job done being a library junkie.&quot;<span style="font-size: large;"><br />
</span></p>
<p>&nbsp;</p>
<p>&nbsp;<span style="font-size: large;"><strong>Janko Javanovic</strong></span></p>
<p><img width="200" vspace="10" hspace="10" height="200" align="left" style="padding: 10px;" alt="" src="http://addyosmani.com/blog/wp-content/uploads/janko.jpg" />&quot;I will be glad to give an opinion about js/jQuery. I agree that developers should have at least some understanding of JavaScript. jQuery and similar libraries might be a good way to get people interested in JavaScript in general. But in the other hand it might actually be a stumbling block &#8211; it&#8217;s so easy to use jQuery that many don&#8217;t need a deeper understanding of JS.</p>
<p>Lack of knowledge become obvious when developers need to modify/extend some jQuery code. They don&#8217;t understand it fully. And then they cry for help. So I&#8217;d say it would be better for them to learn a few things about JS and try figure things out themselves. Otherwise, chances are they will never learn.&quot;</p>
<p><span style="font-size: large;"><strong>Marco Kuiper</strong></span></p>
<p><img width="200" vspace="10" hspace="10" height="200" align="left" style="padding: 10px;" alt="" src="http://addyosmani.com/blog/wp-content/uploads/marco.jpg" /></p>
<p>&quot;Very interesting topic, especially since I learned jQuery before learning actual JavaScript myself (the only JavaScript I knew before that were the &quot;mouse following clock&quot; and &quot;disable right click&quot; stuff &#8211; you know, back in the days&#8230;). But at the time I started to get a hang of jQuery, I did wanted to learn more about JavaScript. Why? Basically because I wanted to get the maximum out of jQuery.</p>
<p>For example, take a look at speed. I learned that using an ID selector ( $(&quot;#myelement&quot;) ) is around 100 times faster than a class selector ( $(&quot;.myelement&quot;) ) . This is because the ID selector maps to the native &quot;getElementByID()&quot; JavaScript function. The same is for using tag selectors ( $(&quot;body&quot;) ), since that maps to the native &quot;getElementsByTagName()&quot; JavaScript function. The class selector loops through all the objects in the DOM to find the specified elements. There are more speed tricks for jQuery that you would know if you knew how JavaScript works.</p>
<p>I truly love the way how you can create elements in jQuery 1.4; Simply apply an element name to the jQuery core method and you&#8217;re ready to go ( $(&quot;&lt;div /&gt;&quot;) ). Before that way was implemented in jQuery, you could simply use the native JavaScript method &quot;document.createElement()&quot;. Although it isn&#8217;t as great as the way jQuery creates elements, it did allow you to create elements using JavaScript. Using those kind of methods even before it was added to the jQuery core, and you were sure to be one step ahead of the rest.</p>
<p>The last example I want to show you, is the use of parameters. The jQuery &quot;.bind()&quot; method attaches behavior to a specified element. The first parameter is the eventType, like &quot;click&quot;, &quot;mouseenter&quot; and &quot;mouseleave&quot;. How would you know which eventTypes you could specify, if you don&#8217;t know the implementation of JavaScript (&quot;object.onmouseenter&quot; etc.)? And why do you use all those curly brackets (when creating objects) and using functions as parameters ( $(&quot;#myelement&quot;).click(function() { var doStuff; } ); )? All that kind of stuff is JavaScript that you&#8217;re using. If you&#8217;re using it, use it and learn it correct.</p>
<p>So, if you want to get better at jQuery, take a look &quot;under the hood&quot; to see the JavaScript engine that is working hard for you. Learn and master it!&quot;</p>
<p>&nbsp;</p>
<p><span style="font-size: large;"><strong>James Padolsey</strong></span></p>
<p><img width="200" vspace="10" hspace="10" height="200" align="left" style="padding: 10px;" alt="" src="http://addyosmani.com/blog/wp-content/uploads/james.jpg" /></p>
<p>&quot;I agree that there is a problem with the terminology used throughout the jQuery community. jQuery uniquely appeals to people with minimal programming experience and that makes it all the more complicated to explain it in the right way. I&rsquo;ve always been a proponent of learning JavaScript and the DOM API before any library. I&rsquo;m not saying that this is the only way; obviously there are people who have learnt it the other way round, and they&rsquo;re just as proficient as you or I. But, still, there is a difficulty to overcome, and I think there&rsquo;s a lot of progress that needs to be made in the way that jQuery&rsquo;s paradigms and implementation are explained to beginners. I think you&rsquo;re right about &ldquo;wrapping&rdquo; being misleading, but, it is a helpful metaphor which gives beginners confidence when using jQuery.</p>
<p>Let&rsquo;s face it! Most people that use jQuery are not looking to become an expert. They don&rsquo;t care about its internals or what really happens when they call the magical dollar. They just want it to work. The people who really want to dig into it will do so by their own means.</p>
<p>I&rsquo;ve used some of these metaphors myself, such as &ldquo;caching&rdquo; &mdash; it can be misleading but most &ldquo;web&rdquo; people understand the term. If I said &ldquo;reference&rdquo; instead then I&rsquo;d have to explain what a reference is and how objects in JavaScript are different to primitive types, and how an identifier is simply a pointer to an object in &ldquo;memory&rdquo;&hellip; it gets complicated very quickly.</p>
<p>jQuery is one of those cool technologies that gives you tremendous power right off the bat! That&rsquo;s what appeals to beginners, I think. They feel powerful; they feel that they pretty-much understand jQuery and how it works. We wouldn&rsquo;t want to shatter that confidence by the realisation that they, in fact, still have a lot to learn about jQuery, JavaScript, and the DOM.</p>
<p>Don&rsquo;t get me wrong. I really do support the idea that beginners should be taught correctly right from square one, but I think it needs to be done delicately. You can&rsquo;t drown them with complicated words and ideas; that&rsquo;ll surely scare them away.&quot;</p>
<p>I would say that learning APIs and abstractions such as the DOM, jQuery, Mootools, Prototype, Dojo, ExtJS and others is quite pointless for the beginner because there are literally thousands of libraries, toolkits and frameworks that the beginner _could_ pick up&#8230;. Obviously the DOM is central but at the end of the day it&#8217;s just a bunch of method-names and argument signatures that you have to remember.</p>
<p>What needs to be taught is **JavaScript** &#8230; or rather, ECMAScript.. the real thing &#8212; i.e. hand out a copy of the ES3 specification. (actually, that&#8217;d definitely scare people)</p>
<p>The problem with learning JS is that most of the really good resources are simply not geared towards beginners. Websites like w3schools and &quot;javascript.about.com&quot; ARE geared towards beginners but their content is crap &#8212; it&#8217;ll get an individual past the noob phase but not any further. They need to be learning about primitives, objects, variable declaration, function declaration, function expressions, scope, closure, operator precedence, booleans&#8230; blah blah blah&#8230; The rhino book is what they all really need.</p>
<p>Most of the people using jQuery don&#8217;t even want to &quot;program&quot;, per se; they just want to add cool&#8217;ness to their websites. Telling them they defined a global instead of a local var, and they should probably use function declarations because it makes debugging easier is pointless because they really couldn&#8217;t care less. The devs that really care about code quality and all the stuff we love are already on the path to enlightenment.&quot;</p>
<p>&nbsp;</p>
<p><strong><span style="font-size: large;">Conclusions</span></strong></p>
<p>And that&#8217;s it guys. What are your thoughts on the topic?. Do <em>you</em> think that it&#8217;s important to have an understanding of basic JavaScript?. We&#8217;d all love to have an open discussion on this and we&#8217;d really like to hear your opinions in the comments section below.  If you liked the post, remember that you can easily share it with your friends and colleagues just be clicking the <a class="retweet" href="http://addyosmani.com/blog/javascriptuncovered/">jQuery Vs. JavaScript &#8211; The Masters Share Their Views On Vanilla JS</a> button. I hope you found this post useful guys!</p>
<p>.</p>
]]></content:encoded>
			<wfw:commentRss>http://addyosmani.com/blog/javascriptuncovered/feed/</wfw:commentRss>
		<slash:comments>31</slash:comments>
		</item>
		<item>
		<title>Extending CSS with jQuery &#8211; A New Year&#8217;s Guide</title>
		<link>http://addyosmani.com/blog/extending-css-with-jquery-a-new-years-guide/</link>
		<comments>http://addyosmani.com/blog/extending-css-with-jquery-a-new-years-guide/#comments</comments>
		<pubDate>Sat, 02 Jan 2010 16:53:50 +0000</pubDate>
		<dc:creator>Addy</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[css transforms]]></category>
		<category><![CDATA[css3]]></category>
		<category><![CDATA[extend css]]></category>
		<category><![CDATA[extend css with jquery]]></category>
		<category><![CDATA[extending css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery css]]></category>
		<category><![CDATA[jquery tips]]></category>
		<category><![CDATA[jquery tools]]></category>

		<guid isPermaLink="false">http://addyosmani.com/blog/extending-css-with-jquery-a-new-years-guide/</guid>
		<description><![CDATA[&#160; &#160; &#160; Preface: Chris Coyier, of CSS-Tricks fame, recently gave an excellent presentation on how to extend CSS using jQuery. I remember seeing this wonderful presentation floating around recently but hadn&#8217;t been aware of who the author was until now. I would like to apologize to every one of my readers, including Chris, for [...]]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/extending.jpg"><img title="page0" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" alt="page0" border="0" src="http://addyosmani.com/blog/wp-content/uploads/extending.jpg" /></a></p>
<p>&nbsp;</p>
<p><span style="color: rgb(51, 51, 51); "><strong>Preface: </strong>Chris Coyier, of CSS-Tricks fame, recently gave an excellent </span><a href="http://www.scribd.com/doc/23649844/Using-jQuery-to-Extend-CSS"><span style="color: rgb(51, 51, 51); ">presentation</span></a><span style="color: rgb(51, 51, 51); "> on how to extend CSS using jQuery. I remember seeing this wonderful presentation floating around recently but hadn&rsquo;t been aware of who the author was until now. I would like to apologize to every one of my readers, including Chris, for posting this article up without any reference to the slides author &ndash; my original online source didn&rsquo;t include any mention of him (or any author for that matter) so I had rather foolishly gone ahead with the use of them without investing more time into researching who put them up. This revised version of the article gives full credit to Chris who is indeed the man that gave the original presentation and was kind enough to give his permission for his slides to be used. I and this article would like to thank him for being the main inspiration behind this topic.</span></p>
<p>&nbsp;</p>
<p>Having viewed Chris&rsquo;s presentation, I thought I&rsquo;d write a post around it to help my readers see how jQuery helps us to extend CSS in ways that make our jobs a whole lot easier. I&rsquo;ve always thought that jQuery was extraordinary framework &#8211; allowing both web developers and designers an easy way to harness the full visual capabilities of JavaScript. One of the nicest advantages of jQuery has been ability to extend CSS beyond what was previously capable with simple mark-up alone. In this post, I&rsquo;m going to show you how to use jQuery to fix cross browser compatibility issues, solve some shortcomings with CSS, do things CSS just can&rsquo;t do on it&rsquo;s own and we&rsquo;ll finally also go through some real-world problems that will be useful to both those new to the framework and those coders who&rsquo;ve been using it for a while. I hope you find it helpful!.</p>
<p>&nbsp;</p>
<p><span id="more-356"></span></p>
<p>Before we leap in, let&rsquo;s take a look at <strong>why</strong> jQuery has come to be the most widely adopted of JavaScript frameworks. First, it&rsquo;s extremely designer friendly. In Chris&rsquo;s slide below you&rsquo;ll see some standard CSS mark-up &ndash; hover conditions, list elements &ndash; defining styles is very much a static process, but jQuery allows you to easily and instantly add or remove classes, conditions, functions and more to any element on your page so that it takes CSS to the next level &#8211; interactivity. jQuery also has a <strong>very</strong> short learning curve (which as some of us know) has meant that creating effects like boxes sliding up or down a page child&rsquo;s play.</p>
<p>&nbsp;</p>
<p>&nbsp;<a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page1.jpg"><img title="page-1" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-1" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page1_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Selectors and Pseudo Selectors</strong></p>
<p>&nbsp;</p>
<p>Selectors are one of the most important aspects of the jQuery library &#8211; these such selectors use familiar CSS syntax to allow page authors to quickly and easily identify any set of page elements to operate upon with the jQuery library methods. Understanding jQuery selectors is the key to using the jQuery library most effectively. Below you&#8217;ll see that an example of where in the past we would have used CSS selectors to define some styling for say, an input element.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><img title="page-6" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-6" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page6_thumb.jpg" /></p>
<p>&nbsp;</p>
<p>jQuery allows us to take even further control of this, providing methods where you can define custom selectors of your own, or use powerful pseudo selectors such as : odd, :button, :submit or :reset to select elements you wish to style. What this means is that at the end of the day, you have access to (or the ability to create access to) any element in the DOM via any number of conditions you want it to satisfy.</p>
<p>&nbsp;</p>
<p><img title="page-7" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-7" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page7_thumb.jpg" /></p>
<p>&nbsp;</p>
<p><strong>Transparency</strong></p>
<p>&nbsp;</p>
<p>jQuery has also made Opacity (or Transparency) more easy to implement than ever. Opacity is an effect whereby you set the visibility of an element to be a percentage or fraction of what it originally was &ndash; in design, this is often used to show that an element is inactive or to allow you to see through it. In the past, it was necessary to code up a special Opacity case for IE8, IE5-7, Safari, Netscape and the list went on. It was frustrating for developers because even at their best, these custom cases didn&rsquo;t always work. That&rsquo;s where we bring in jQuery&hellip;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a><img title="page-2" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-2" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page2_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p>jQuery&rsquo;s support for transparency is so broad, that all you need to do to add opacity effects to any element in your page is use a selector for the element followed by .css(&ldquo;opacity&rdquo;, &ldquo;opacity-value&rdquo;); It really is that straight-forward.</p>
<p>&nbsp;</p>
<p><a><img title="page-3" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-3" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page3_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p><strong>Hover</strong></p>
<p>&nbsp;</p>
<p>Next, let&rsquo;s take a look at the hover class. I remember that years ago, you could probably get away with using the simple css :hover selector to add hover effects to an element, but you would always run into problems with older browsers not being able to use this correctly. Now before anyone leaps in and says.. Well.. nobody these days has an old browser, you might be right, but don&rsquo;t forget about that Mobile Web Browsers. Many of these browsers currently used in all sorts of Mobile devices don&rsquo;t fully support the entire CSS 1/2 specification, but using jQuery can provide us with access to the features of them than can make &lsquo;hover&rsquo; work as expected.</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page4.jpg"><img title="page-4" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-4" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page4_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p>With jQuery, all you need to do to add hover classes to any element on your page is define a function as follows $(selector).hover { function for mouseOver, function for mouseOut}. Your mouseOver function can easily attach a class that highlights the element or perhaps underlines it while your mouseOver event would remove this class taking the element back to it&rsquo;s default state. No more having to worry about IE not supporting the effect we&rsquo;re trying to achieve!</p>
<p>&nbsp;</p>
<p>&nbsp; <a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page5.jpg"><img title="page-5" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-5" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page5_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Animation</strong></p>
<p>&nbsp;</p>
<p>For anyone that&rsquo;s ever seen some of my CoverFlow work in JavaScript over the years, you&rsquo;ll know that it can sometimes take a while to create impressive effects using JavaScript if you&rsquo;re writing them from scratch. You don&rsquo;t have the Tweening tools available to you in many production quality animation systems and much of your work is going to be manual. You&rsquo;ve got to consider how the effect is going to look using your default CSS, whether it&rsquo;s going to slow your browser down to a halt when all your styling&rsquo;s done, if it&rsquo;ll work in all browsers but..jQuery takes a lot of that pain right away.</p>
<p>&nbsp;</p>
<p>With it&rsquo;s built-in functions like fadeIn(), slideDown() and more, it&rsquo;s never been easier to add simple animated effects to you webpages easily. jQuery even comes with a special animate() function that lets you chain together special effects so things that I would have had nightmares about (like having a logo from the top of a page, adding and removing CSS classes every second or two to create complex effects or even just making an icon bounce) are now relatively easy to get done in a few minutes.</p>
<p>&nbsp;</p>
<p><img title="page-17" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-17" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page17_thumb.jpg" /></p>
<p>&nbsp;</p>
<p>In this slide you can see an example of where jQuery can be truly powerful &ndash; we have a block of text in a paragraph which we need to constrain to a specific width so that it doesn&rsquo;t break our site&rsquo;s template. This piece of text could just be a list that would look better in it&rsquo;s original form rather than having a few line breaks split it up, so how can jQuery help me come up with a solution to my problem?.</p>
<p>&nbsp;</p>
<p>Well, let&rsquo;s use hover! &ndash; if I were to make it so that when someone hovers over the paragraph it&rsquo;ll set itself to the front of the page and expand, they&rsquo;ll be able to read it without breaking my template. Just as easily, when they mouseOut, I can simply set the width of the paragraph back to what it was before and my page will still work fine without any problems at all. A perfect example of how a nice UI update can be achieved with minimal code and the amazing jQuery framework.</p>
<p>&nbsp;</p>
<p><img title="page-18" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-18" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page18_thumb.jpg" /></p>
<p>&nbsp;</p>
<p><strong>Grids</strong></p>
<p>&nbsp;</p>
<p>Next lets take a look at Grids &ndash; Grids can contain all sorts of information from gallery thumbnails to previews of pieces of text from a document. A perfect example of the kind of content you might want to fit into the below layout is if you have a Blog or Publishing site where users are posting new articles or stories every day (even Tweets). You might want a way of showing readers a preview of content past just the title and what better way to display it in a compact form other than a grid?</p>
<p>&nbsp;</p>
<p>The problem with grids are that they can be difficult &ndash; when you&rsquo;re not dealing with tabular data and your content has text of a length that can vary, you need to think of an intelligent way to make your layout shape around the data so that it still looks the way you want it to.</p>
<p>&nbsp;</p>
<p><a><img title="page-8" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-8" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page8_thumb.jpg" /></a></p>
<p>&nbsp;</p>
<p>If we try to solve this problem using just CSS, it&rsquo;s going to take us hours because as you can see below, simply defining one class for our grid entry isn&rsquo;t going to solve our problem. We *could* do something on the server side such as limiting the amount of text that&rsquo;s output, but what if we wanted to let the user expand the entry so they can read all of the preview text or we wanted to display a complete sentence? A UI Solution would be called for and as CSS is going to cause us some problems, let&rsquo;s see if we can come up with something more intelligent using jQuery next..</p>
<p>&nbsp;</p>
<p><a><img title="page-9" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-9" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page9_thumb.jpg" /></a>&nbsp;</p>
<p>With jQuery, we can set specific CSS rules for border cases, text that makes our preview element longer than the other entries, we can turn our entire DIV into a clickable object and to top if off we can also add some nice opacity hover effects to make it look that extra bit professional. We can achieve all of these using a few simple checks for height, a few css rules and some basic jQuery functions. For the full code samples, please click on the slide to expand.</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page10.jpg"><img title="page-10" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-10" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page10_thumb.jpg" /></a>&nbsp;</p>
<p><strong>Zebra Striping</strong></p>
<p>&nbsp;</p>
<p>Zebra Striping is a design technique that will be familiar to anyone that&#8217;s ever used iTunes &#8211; it&#8217;s an aesthetically pleasing way of displaying your information because it allows the reader to neatly separate rows of data in their mind.</p>
<p>&nbsp;</p>
<p>The problem with Zebra striping is that it usually requires a lot of manual work, whether you decide to use the <img src='http://addyosmani.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> dd and :even selectors or not. Stylesheets, whether they be on the web via CSS or in an application such as Adobe InDesign are there to control the visual properties of elements contained within a document. And while CSS&rsquo;s selectors allow you to specify which document elements should have which styles applied, the pure-CSS solution to Zebra Striping requires the author to apply the appropriate class (either even or odd) to at least half of the rows in a table.</p>
<p>&nbsp;</p>
<p>While this isn&rsquo;t a problem if the table contains only a few rows, copying and pasting can get old very quickly when the table contains tens or hundreds of rows. This problem can be avoided when the table is generated on the server side, because modifications to the table&rsquo;s markup need only the editing of a few lines of code. Unfortunately, not every web page is built by a server-side application, and even if you are creating a web app, it is frequently desirable to off-load layout processing to the client through CSS and JavaScript &#8211; so let&#8217;s see how jQuery can make Zebra striping easy for everyone.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page12.jpg"><img title="page-12" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-12" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page12_thumb.jpg" /></a>&nbsp;</p>
<p>Rather than tediously and manually binding CSS classes to the appropriate rows, we can instead use a little jQuery to go through the table and apply CSS styles for us.</p>
<p>&nbsp;</p>
<p>Let&rsquo;s take a look at how this can be achieved:</p>
<p>&nbsp;</p>
<p>1. Obtain a reference to the table that we want to add stripes to.    <br />
2. With that in hand, drill down into the table&rsquo;s child elements to find all the &lt;tr&gt;s that are contained within &lt;tbody&gt; elements.     <br />
3. Loop through the &lt;td&gt;s and apply the appropriate styling (which is determined by the location of this &lt;tr&gt; in the table).     <br />
Season to taste.</p>
<p>&nbsp;</p>
<p>A-List-Apart has an excellent demo of this that reproduces the <a href="http://www.alistapart.com/d/stripedtables/example.html">iTunes Zebra Striping Effect.</a> The source code to their script can be found <a href="http://www.alistapart.com/d/stripedtables/script.txt">here</a>. In the below example, Zebra Striping can be extended by adding hover classes for specific columns or rows.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a><img title="page-13" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-13" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page13_thumb.jpg" /></a>&nbsp;</p>
<p>We can take that example one step further by using a selector which checks to see if a particular row contains a specific term. This Client-side search doesn&rsquo;t need Ajax or any server-side code to work and within one line of jQuery we can easily find and highlight the row we&rsquo;re looking for.</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page14.jpg"><img title="page-14" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-14" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page14_thumb.jpg" /></a>&nbsp;</p>
<p>A plugin that I&rsquo;ve personally found useful in the past is the jQuery TableSorter by Christian Bach. It&rsquo;s very easy to integrate into both HTML, Adobe AIR and PHP Applications and even includes some really great Sorting options too. You can check it out over at <a href="http://www.tablesorter.com/docs">http://www.tablesorter.com/docs</a></p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page15.jpg"><img title="page-15" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-15" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page15_thumb.jpg" /></a>&nbsp;</p>
<p>&nbsp;</p>
<p><strong>Loading After Loading</strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>One of the issues that users regularly experience is having to wait for their page to fully load up Flash Video Players &ndash; we&rsquo;ve all had to deal with this crap before. Some sites, rather than automatically loading up a video, will instead display a thumbnail or preview image in the Player to speed things up, but if you have to wait for Flash to initialize it&rsquo;s still going to take some of your time. jQuery allows us to do this a little more intelligently. Rather than loading up multimedia when the page first loads, just display the preview image in a div (without the Player loaded). If a user decided they want to watch the video, you can easily load up the Flash Video Player page via jQuery&rsquo;s .load() function so that if they would like to watch it..they will, but if they don&rsquo;t, they&rsquo;ll have a much faster and smoother experience browsing through the rest of your site.</p>
<p>&nbsp;</p>
<p>&nbsp; <a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page19.jpg"><img title="page-19" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-19" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page19_thumb.jpg" /></a>&nbsp;</p>
<p><strong>Controlling Outside Content</strong></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>CSS is a wonderful tool for designing, but what if you want to control the content that you&rsquo;ve styled up outside of pure CSS definitions?. That&rsquo;s a problem&nbsp; &#8211; CSS doesn&rsquo;t allow you to dynamically display content when other actions occur in your page such as a user entering in their password or credit-card details. This is where some really simply jQuery logic can add more power to your interface.&nbsp; In the example below we want to display a voucher code box when a user clicks on &ldquo;Add a coupon&rdquo;. Because CSS doesn&rsquo;t allow us to do this..</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page20.jpg"><img title="page-20" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-20" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page20_thumb.jpg" /></a>&nbsp;</p>
<p>we simply do it with some JavaScript. Notice how in just two lines of code we&rsquo;ve managed to enrich our shopping cart in less than 30 seconds. How awesome is that?</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page21.jpg"><img title="page-21" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-21" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page21_thumb.jpg" /></a>&nbsp;</p>
<p>To sum it up, jQuery is a CSS fan&rsquo;s dream &ndash; it provides you with a simple, elegant way to add another level of interaction to your pages without having to waste your time debugging too many quirks with our age-old friend, JavaScript. On many occasions, it&rsquo;s helped me to pump out impressive interactive web-pages that not only impressed my clients&hellip;.. they impressed <em>me</em> too, because for many years it wasn&rsquo;t possible to do the things it&rsquo;s enabled us to without taking days. So as we welcome in the new year, I&rsquo;d like to take my hat off to jQuery &ndash; thank you for making my life easier. Once again, my thanks to Chris Coyier for his excellent presentation slides</p>
<p>&nbsp;</p>
<p><a class="thickbox" href="http://addyosmani.com/blog/wp-content/uploads/2009/12/page22.jpg"><img title="page-22" style="border-top-width: 0px; display: block; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; border-right-width: 0px" height="400" alt="page-22" width="526" border="0" src="http://addyosmani.com/blog/wp-content/uploads/2009/12/page22_thumb.jpg" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://addyosmani.com/blog/extending-css-with-jquery-a-new-years-guide/feed/</wfw:commentRss>
		<slash:comments>20</slash:comments>
		</item>
		<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>
		<item>
		<title>25 Ways To Build Better jQuery Web Applications</title>
		<link>http://addyosmani.com/blog/25-ways-to-build-better-jquery-web-applications/</link>
		<comments>http://addyosmani.com/blog/25-ways-to-build-better-jquery-web-applications/#comments</comments>
		<pubDate>Sat, 19 Sep 2009 22:09:43 +0000</pubDate>
		<dc:creator>Addy</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript web applications]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[jquery application]]></category>
		<category><![CDATA[jquery build]]></category>
		<category><![CDATA[jquery builders]]></category>
		<category><![CDATA[jquery carousels]]></category>
		<category><![CDATA[jquery examples]]></category>
		<category><![CDATA[jquery tips]]></category>
		<category><![CDATA[jquery tricks]]></category>
		<category><![CDATA[web application builders]]></category>
		<category><![CDATA[web application helpers]]></category>
		<category><![CDATA[web application javascript]]></category>
		<category><![CDATA[web applications]]></category>

		<guid isPermaLink="false">http://addyosmani.com/blog/?p=75</guid>
		<description><![CDATA[25 Ways To Build Better jQuery Web Applications]]></description>
			<content:encoded><![CDATA[<p><a title="View 25 Ways To Build Better jQuery Web Applications on Scribd" href="http://www.scribd.com/doc/19952433/25-Ways-To-Build-Better-jQuery-Web-Applications" style="margin: 12px auto 6px auto; font-family: Helvetica,Arial,Sans-serif; font-style: normal; font-variant: normal; font-weight: normal; font-size: 14px; line-height: normal; font-size-adjust: none; font-stretch: normal; -x-system-font: none; display: block; text-decoration: underline;">25 Ways To Build Better jQuery Web Applications</a> <object codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" id="doc_201373502024667" name="doc_201373502024667" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" align="middle"	height="400" width="450" ><param name="movie"	value="http://d1.scribdassets.com/ScribdViewer.swf?document_id=19952433&#038;access_key=key-1cxlhciwasaubwdnzs23&#038;page=1&#038;version=1&#038;viewMode=list"><param name="quality" value="high"><param name="play" value="true"><param name="loop" value="true"><param name="scale" value="showall"><param name="wmode" value="opaque"><param name="devicefont" value="false"><param name="bgcolor" value="#ffffff"><param name="menu" value="true"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><param name="salign" value=""><param name="mode" value="list"><embed src="http://d1.scribdassets.com/ScribdViewer.swf?document_id=19952433&#038;access_key=key-1cxlhciwasaubwdnzs23&#038;page=1&#038;version=1&#038;viewMode=list" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" play="true" loop="true" scale="showall" wmode="opaque" devicefont="false" bgcolor="#ffffff" name="doc_201373502024667_object" menu="true" allowfullscreen="true" allowscriptaccess="always" salign="" type="application/x-shockwave-flash" align="middle" mode="list" height="400" width="450"></embed></object></p>
]]></content:encoded>
			<wfw:commentRss>http://addyosmani.com/blog/25-ways-to-build-better-jquery-web-applications/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
