<?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>Girish Gangadharan &#187; JavaScript</title>
	<atom:link href="http://giri.sh/category/javascript/feed/" rel="self" type="application/rss+xml" />
	<link>http://giri.sh</link>
	<description>Did you expect something witty here?</description>
	<lastBuildDate>Wed, 11 Jan 2012 05:54:25 +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>My talk on jQuery at Dallas TechFest 2011</title>
		<link>http://giri.sh/2011/08/13/my-talk-on-jquery-at-dallas-techfest-2011/</link>
		<comments>http://giri.sh/2011/08/13/my-talk-on-jquery-at-dallas-techfest-2011/#comments</comments>
		<pubDate>Sat, 13 Aug 2011 23:28:19 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Presentation]]></category>

		<guid isPermaLink="false">http://giri.sh/?p=202</guid>
		<description><![CDATA[Not sure if many people know this but this was my first talk at an official conference. I&#8217;ve presented several times before at work and also at user groups. But not at any conference before this one. So I&#8217;d like to thank Tim Rayburn and Teresa Burger for the opportunity. I&#8217;d completely forgotten that I&#8217;d [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>Not sure if many people know this but this was my first talk at an official conference. I&#8217;ve presented several times before at work and also at user groups. But not at any conference before this one.</p>
<p>So I&#8217;d like to thank <a title="Tim Rayburn on Twitter" href="https://twitter.com/#!/TRayburn">Tim Rayburn</a> and <a title="Teresa Burger on Twitter" href="https://twitter.com/t_burger">Teresa Burger</a> for the opportunity. I&#8217;d completely forgotten that I&#8217;d even applied for it on <a title="Dallas TechFest" href="http://dallastechfest.com" target="_blank">dallastechfest.com</a> literally months back. One fine day, I get an email from Teresa that they&#8217;d love to have me come speak and that&#8217;s how this whole thing happened.</p>
<p>The talk went pretty well actually. I was surprised to see a full house, to be honest. They took pictures of both me and the audience. I was told that I can have them in a couple of weeks. I&#8217;ll post them here as soon as I get them.</p>
<p>Also, it was good to see my friend Rob Pierry again. He too did a talk on StreamInsight. He also had a little surprise for me at the end of his presentation. See below.</p>
<p><a title="Rob Pierry's little shout out" href="http://giri.sh/wp-content/uploads/2011/08/372161844.jpg"><img src="http://giri.sh/wp-content/uploads/2011/08/372161844.jpg" alt="Rob Pierry's little shout out" width="500" /></a></p>
<p>Thanks to everyone who showed up. I hope you found it useful.</p>
<p>Anyways, here are the slides from the presentation.</p>
<div id="__ss_8845379" style="width: 425px;"><strong style="display: block; margin: 12px 0 4px;"><a title="jQuery - 10 Time-Savers You (Maybe) Don't Know" href="http://www.slideshare.net/girish82/jquery-10-timesavers-you-maybe-dont-know" target="_blank">jQuery &#8211; 10 Time-Savers You (Maybe) Don&#8217;t Know</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/8845379" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="425" height="355"></iframe></p>
<div style="padding: 5px 0 12px;">View more <a href="http://www.slideshare.net/" target="_blank">presentations</a> from <a href="http://www.slideshare.net/girish82" target="_blank">girish82</a></div>
</div>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2011/08/13/my-talk-on-jquery-at-dallas-techfest-2011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My presentation at CTXDNUG</title>
		<link>http://giri.sh/2011/05/13/my-presentation-at-ctxdnug/</link>
		<comments>http://giri.sh/2011/05/13/my-presentation-at-ctxdnug/#comments</comments>
		<pubDate>Fri, 13 May 2011 23:26:46 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://giri.sh/?p=192</guid>
		<description><![CDATA[I had the opportunity to do a presentation on jQuery at Central TX .Net User Group yesterday. It was fun. Friendly organizers. Good weather (helped the 3 hour drive) . Pretty decent turnout. It was an interactive session and there were some interesting questions asked and answered. I tried something new this time that I [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>I had the opportunity to do a presentation on jQuery at Central TX .Net User Group yesterday. It was fun. Friendly organizers. Good weather (helped the 3 hour drive) . Pretty decent turnout. It was an interactive session and there were some interesting questions asked and answered.</p>
<p>I tried something new this time that I hadn&#8217;t done before. I demonstrated some of the tips I&#8217;d talked about, <strong><em>on a live site</em></strong> &#8211; <a title="MSDN" href="http://msdn.microsoft.com" target="_blank">msdn.microsoft.com</a></p>
<p>That&#8217;s when the audience came alive. Some of them had never seen <a title="Firebug" href="http://getfirebug.com" target="_blank">Firebug</a> before and they were just fascinated when I was making changes &#8220;live&#8221; on the site directly. We digressed a little bit as I started showing them some of the other cool features of Firebug besides the JavaScript console.</p>
<p>Just based on the reaction of the people there, it&#8217;s probably worthwhile to do an entire presentation just on the web development tools that come integrated with (or can be added on to) the browser. That would include not just <a title="Firebug" href="http://getfirebug.com" target="_blank">Firebug</a>, but also <a title="Dragonfly" href="http://www.opera.com/dragonfly/" target="_blank">Dragonfly</a>, <a title="Chrome Developer Tools" href="http://code.google.com/chrome/devtools/docs/overview.html" target="_blank">Chrome Developer Tools</a>, <a title="IE Developer Tools" href="http://msdn.microsoft.com/en-us/library/gg589512%28v=VS.85%29.aspx" target="_blank">IE Developer Tools</a> and whatever that thing is that comes with Safari. <img src='http://giri.sh/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Anyways, I&#8217;ve <a title="jQuery - Doing it right" href="http://www.slideshare.net/girish82/jquery-doing-it-right" target="_blank">uploaded my presentation</a> to <a title="SlideShare" href="http://slideshare.net" target="_blank">SlideShare</a>. Here&#8217;s the direct URL &#8211; <a href="http://www.slideshare.net/girish82/jquery-doing-it-right">http://www.slideshare.net/girish82/jquery-doing-it-right</a></p>
<p>And for quick reference, I&#8217;ve embedded it below as well.</p>
<div style="width:425px" id="__ss_7958830"> <strong style="display:block;margin:12px 0 4px"><a href="http://www.slideshare.net/girish82/jquery-doing-it-right" title="jQuery - Doing it right">jQuery &#8211; Doing it right</a></strong> <iframe src="http://www.slideshare.net/slideshow/embed_code/7958830" width="425" height="355" frameborder="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
<div style="padding:5px 0 12px"> View more <a href="http://www.slideshare.net/">presentations</a> from <a href="http://www.slideshare.net/girish82">girish82</a> </div>
</p></div>
<p>Feel free to post any questions/comments/corrections or any feedback in the comments below.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2011/05/13/my-presentation-at-ctxdnug/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit testing for JavaScript using Firebug</title>
		<link>http://giri.sh/2011/01/07/unit-testing-for-javascript-using-firebug/</link>
		<comments>http://giri.sh/2011/01/07/unit-testing-for-javascript-using-firebug/#comments</comments>
		<pubDate>Fri, 07 Jan 2011 19:54:55 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[Firefox]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://giri.sh/?p=181</guid>
		<description><![CDATA[Firebug + JQuery + Unit Testing = http://ejohn.org/blog/fireunit/ Sweet. No related posts.


No related posts.]]></description>
			<content:encoded><![CDATA[<p><a title="Firebug" href="http://getfirebug.com">Firebug </a>+ <a title="jQuery" href="http://jquery.com">JQuery</a> + <a title="Unit testing" href="http://en.wikipedia.org/wiki/Unit_testing">Unit Testing</a> = <a href="http://ejohn.org/blog/fireunit/">http://ejohn.org/blog/fireunit/</a></p>
<p>Sweet.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2011/01/07/unit-testing-for-javascript-using-firebug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>10 useful tips and tricks with jQuery</title>
		<link>http://giri.sh/2010/09/29/10-useful-tips-and-tricks-with-jquery/</link>
		<comments>http://giri.sh/2010/09/29/10-useful-tips-and-tricks-with-jquery/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 03:06:21 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://giri.sh/?p=150</guid>
		<description><![CDATA[jQuery is an awesome JavaScript library that simplifies cross browser client side scripting especially DOM traversing, AJAX development, event handling, etc. If you are reading this article, chances are that you have already used jQuery at some point of time. If you haven’t, stop reading this right now. Open up a new tab. Go to [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>jQuery is an awesome JavaScript library that simplifies cross browser client side scripting especially DOM traversing, AJAX development, event handling, etc.</p>
<p>If you are reading this article, chances are that you have already used jQuery at some point of time. If you haven’t, stop reading this right now. Open up a new tab. Go to jQuery.com and download it. Play with it. That&#8217;s the best way to learn it.</p>
<p>Now, back to our topic. I obtained most of my jQuery skills on a project that I&#8217;ve been working on for the last couple of years. While I enjoyed the experience of discovering new things and learning it on-the-go, I must admit that I have written pretty bad code without knowing how badly JavaScript sucked.</p>
<p>Good news is that, I’ve learned quite a bit during that process. Not just about jQuery but also about DOM, CSS, JS, hidden cool features of Firebug, JSON, XHR etc. And better news is that I’m about to share some of that knowledge with you in a moment.</p>
<p>In this article, we’ll talk about little things you can do to your jQuery code that will improve performance and also improve readability and hence, maintainability.</p>
<p>You can download the source from <a href="http://giri.sh/wp-content/uploads/2010/09/test1.zip">here</a>.</p>
<p>Anyways, here we go. Code and explanation follows.</p>
<p><strong>1. Chaining</strong></p>
<p>Let’s take a look at the following code snippet.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testControlId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'red'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testControlId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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;">'#testControlId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Google!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Text of this link has changed to : '</span> <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: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>While it’s a perfectly functioning code, it has a couple of issues. For one, it’s a little more verbose than it needs to be. Two, it forces JavaScript to go look up the control by the id “testControlId” every single time it is referenced, which can add up eventually and cause the page to crawl.</p>
<p>Note that most of the jQuery methods returns a jQuery object and in this case, it means the control represented by “testControlId”. So let’s see how we can rewrite the code above.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testControlId'</span><span style="color: #009900;">&#41;</span>
             .<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
                 color<span style="color: #339933;">:</span> <span style="color: #3366CC;">'red'</span>
             <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
             .<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                 $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Google!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                 <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Text of this link has changed to : '</span> <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: #009900;">&#41;</span><span style="color: #339933;">;</span>
             <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Does that make sense? “.css()” function returns a jQuery object that can be worked on instantly. So we use it right away to set the other properties like to hook up the click event handler.</p>
<p><strong>2. Caching</strong></p>
<p>Caching is a great way to limit the number of times we need to traverse the DOM to find elements matched by the selectors. Here’s an example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> anchors <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
anchors.<span style="color: #660066;">click</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>
    anchors.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'cacheClass'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So when would you use caching as opposed to just chaining? Use chains when there is relatively lesser amount of things to do with the object. In other words, if the chain gets too long to the point that it compromises readability, then it&#8217;s better to cache the element using a variable and work with that. Remember, whenever you use chaining, proper indentation and line breaks are necessary to make your code readable and maintainable.</p>
<p><strong>3. Event delegation </strong></p>
<p>Let&#8217;s say you have a requirement to write code to change the background of a table cell when the user hovers over it. Here&#8217;s one way to do it.</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;">'#testTable td'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#ECE9D8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Do you see the problem with the code above? What happens when you have a 100 rows in the table, each with 10 cells ? This code will bind 1000 events to the table. Seems inefficient, isn&#8217;t it?</p>
<p>A better way to do the same thing is to just attach a single event to the table, so that when the user clicks anywhere within the table, the event handler can figure out which cell was clicked before turning it’s background color to ‘#ECE9D8&#8242;. That&#8217;s what the following code does.</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;">'#testTable'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
     <span style="color: #003366; font-weight: bold;">var</span> clicked <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span>e.<span style="color: #660066;">target</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     clicked.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'background'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'#ECE9D8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Another advantage of this code, is that if you add new rows to the table after the initial DOM load, the handler would still get fired even for the new cells.</p>
<p><strong>4. Save data using Data() </strong></p>
<p>jQuery actually provides a method to store data in DOM. So instead of trying to incorrectly use some of the unused attributes like title, alt etc. to save data as Key-Value pair, you can do it as follows.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">//Set the value</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testControlId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'someKey'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'someValue'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">//Get the value</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'#testControlId'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">data</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'someKey'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>5. Custom selectors</strong></p>
<p>What if the built-in selectors in jQuery is not doing it for you? What if you want to select elements in the DOM using some filters that don&#8217;t exist out of the box? You can write your own custom selectors to do that. Take a look at the code below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$.<span style="color: #660066;">extend</span><span style="color: #009900;">&#40;</span>$.<span style="color: #660066;">expr</span><span style="color: #009900;">&#91;</span><span style="color: #3366CC;">':'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
      withEmptyHREF<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">return</span> $<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">''</span> <span style="color: #339933;">||</span> $<span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">attr</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'href'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #3366CC;">'undefined'</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'a:withEmptyHREF'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">css</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'color'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'green'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What this code does is, go find all the anchor tags whose href is not set and change the color to green. Obviously, this is a very simple example. But as you can imagine, this feature has the potential to do a lot of powerful stuff.</p>
<p><strong>6. Use Context</strong></p>
<p>Instead of starting from the DOM root, it looks for the elements matching the selector in the Context provided as shown below.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;a.someAnchorClass&quot;</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;div.someClass&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This significantly cuts down on the time it takes to find an element because we have narrowed down the look up area to the div of type “someClass”. So now, it looks for the anchor tags with class “someAnchorClass” only in that div.</p>
<p><strong>7. Live() Vs Click()</strong></p>
<p>Live() will bind a click handler to all elements matching the selector in the current DOM as well as all new elements (that matches the selector) added to the page later. For example,</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span> <span style="color: #3366CC;">&quot;a&quot;</span> <span style="color: #009900;">&#41;</span>.<span style="color: #660066;">live</span><span style="color: #009900;">&#40;</span>
 <span style="color: #3366CC;">&quot;click&quot;</span><span style="color: #339933;">,</span>
 <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span> event <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
   doSomething<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Click() will bind a handler to all the elements matching the selector in the current DOM. Period. It doesn’t hook the handlers up to the matching elements that get added later on. Here’s an example.</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;">'a'</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">click</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>
    doSomething<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: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong>8. Length() Vs Size()</strong></p>
<p>What if you need to find out if there are any div elements whose class is set to &#8216;hidden&#8217; in the DOM? That&#8217;s when you use the jQuery object’s length property as follows:</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> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.hidden&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">length</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//You'll get in here if there's at least one div of class &quot;hidden&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Guess what? You can use the Size() method to do the same thing. So what’s the difference?</p>
<p>Size() method uses Length() internally. So essentially, you’ll be skipping one layer if you use length() directly and hence, performance will slightly faster.</p>
<p>Here’s an example of Size() function.</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> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;div.hidden&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">size</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//You'll get in here if there's at least one div of class &quot;hidden&quot;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong>9. Partial Page Refresh</strong></p>
<p>If you want to refresh just part of the page, specifically just one control, then here&#8217;s how you do it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">ready</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
 setInterval<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#testControlId&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span>location.<span style="color: #660066;">href</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot; #testControlId&amp;gt;*&quot;</span><span style="color: #339933;">,</span><span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
 <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This may not necessarily replace iFrames (ugh!), Ajax calls etc. But it can be very useful when you have widgets on a page that need to be refreshed without reloading the entire page.</p>
<p><strong>10. Disable right click</strong></p>
<p>This may sound a little silly at first. Why would you want to disable the context menu on a page, right? But think about this. What if you have to add some custom menu of your own to provide certain features? In that case, the browser’s context menu may be obtrusive, yeah? So here’s how you remove it.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span>document<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;contextmenu&quot;</span><span style="color: #339933;">,</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//Add code for your own custom menu if necessary.</span>
    <span style="color: #006600; font-style: italic;">//And now, cancel the default context menu</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></pre></div></div>

<p><strong>Conclusion</strong></p>
<p>jQuery is one of the best things that has happened to web development in the last few years. Suddenly, developers do not loathe JavaScript anymore.</p>
<p>Learn it. Embrace it. Join the cool kids club.</p>
<p><strong>Happy Coding!</strong></p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2010/09/29/10-useful-tips-and-tricks-with-jquery/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Overriding hyperlinks&#8217; default behaviour in javascript</title>
		<link>http://giri.sh/2006/07/06/overriding-hyperlinks-default-behaviour-in-javascript/</link>
		<comments>http://giri.sh/2006/07/06/overriding-hyperlinks-default-behaviour-in-javascript/#comments</comments>
		<pubDate>Fri, 07 Jul 2006 03:14:00 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://morethan2cents.com/morethan2cents/?p=18</guid>
		<description><![CDATA[No biggie, right? Write a javascript function that should do whatever is needed and just hook it up with the OnClick attribute of the hyperlink. Simple and easy. Let&#8217;s say the function looks something like this. &#60;script type="text/javascript"&#62;function HandleClick() { alert("Some message!"); } &#60;/script&#62; Now let&#8217;s test it. Click this link. See what happened? The [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>No biggie, right? Write a javascript function that should do whatever is needed and just hook it up with the OnClick attribute of the hyperlink. Simple and easy.</p>
<p>Let&#8217;s say the function looks something like this.</p>
<p><code>&lt;script  type="text/javascript"&gt;function HandleClick() {<br />
alert("Some message!");<br />
}<br />
&lt;/script&gt;</code></p>
<p>Now let&#8217;s test it.</p>
<p><script  type="text/javascript">function HandleClick() {
alert("Some message!");
}
</script></p>
<p>Click this <a onclick="HandleClick();" href="http://www.morethan2cents.com">link</a>.</p>
<p>See what happened? The javascript function was executed but you still got redirected to the url set in the <code>href</code> attribute of the link.</p>
<p>This is due to the fact that we have overridden the default behaviour of the hyperlink but we still haven&#8217;t told Javascript to cancel the default behaviour.</p>
<p>How do we fix this? Just add <code>return false;</code> right after the function call, as shown below.</p>
<p><code>&lt;a href="http://www.morethan2cents.com" onclick="HandleClick(); return false;"&gt;link&lt;/a&gt;</code></p>
<p>By returning false, you are basically telling javascript that the link was not clicked and thus not to trigger the default behaviour.</p>
<p>Now try it again.  Click this <a onclick="HandleClick(); return false;" href="http://www.morethan2cents.com"> link</a>.</p>
<p>Voila! No redirection. Alternatively, you could leave the href attribute blank (<code>href=&#8221;&#8221;</code>). But that would basically refresh the page for obvious reasons.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2006/07/06/overriding-hyperlinks-default-behaviour-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Smart navigation alternative</title>
		<link>http://giri.sh/2006/05/23/smart-navigation-alternative/</link>
		<comments>http://giri.sh/2006/05/23/smart-navigation-alternative/#comments</comments>
		<pubDate>Wed, 24 May 2006 01:37:00 +0000</pubDate>
		<dc:creator>girish</dc:creator>
				<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://morethan2cents.com/morethan2cents/?p=8</guid>
		<description><![CDATA[So in this project that i&#8217;m working on at my workplace, we have a bunch of products&#8217; details displayed in a grid whose prices can be overridden by the admin user. The user has to check a &#8220;Override&#8221; checkbox, which would enable the price textbox. Uncheck obviously means the price cannot be overridden and textbox [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<p>So in this project that i&#8217;m working on at my workplace, we have a bunch of products&#8217; details displayed in a grid whose prices can be overridden by the admin user. The user has to check a &#8220;Override&#8221; checkbox, which would enable the price textbox. Uncheck obviously means the price cannot be overridden and textbox would be disabled. For business reasons, we had to use the autopostback option for the override checkboxes.</p>
<p>Anyways&#8230;the challenge was that there were atleast 25 or more products displayed in the grid and thus as you can imagine, when the user checked the bottom most product&#8217;s override checkbox the page posted back and auto-scrolled to the top of the page. This could be very annoying to the user and apparently, Smart Navigation was neither smart nor navigated anywhere expected and forced me to rely on Javascript to solve this issue.</p>
<p>After some googling, here&#8217;s how i fixed it.</p>
<p>There&#8217;s a javascript property called <em>location.hash</em></p>
<p>bq. hash is a property of both the Link and the Location objects and is a string beginning with a hash (#). It specifies an anchor name in an HTTP URL.</p>
<p>For example, if the url is something like http://www.example.com#anchor</p>
<p>then #anchor is the hash property.</p>
<p>What this means is that if you have the id of an element in the page, you can set the focus to that element, when the page loads by using the location.hash property.</p>
<p>If you are using the <a href="http://msdn.microsoft.com/workshop/author/dhtml/reference/objects/a.asp"><em>a element</em></a> , then you may use either the id attribute or the name attribute.</p>
<p>So i wrote a little procedure in the aspx&#8217;s code-behind that looks like this:</p>
<pre>Private Sub anchorScript(ByVal productId As String)
     Dim Script As String
     Script &amp;= "" &amp; Environment.NewLine
     Script &amp;= "var anchorLocation='#" &amp; productId &amp; "';" &amp; Environment.NewLine
     Script &amp;= "location.hash=anchorLocation;" &amp; Environment.NewLine
     Script &amp;= "" &amp; Environment.NewLine

     If (Not Page.IsStartupScriptRegistered("anchorScript")) Then
           Page.RegisterStartupScript("anchorScript", Script)
     End If
End Sub</pre>
<p>And called this procedure after doing the necessary stuff in the checkbox&#8217;s CheckedChanged event handler. Typically this would be the last line in the sub.</p>
<p>The above procedure registers a javascript function that when rendered, would look like this :</p>
<pre>
var anchorLocation='#'
location.hash=anchorLocation;
</pre>
<p>This would tell the browser to focus on the element that has an id(passed into the anchorScript procedure).</p>
<p>Simple. Worked like a charm.</p>


<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://giri.sh/2006/05/23/smart-navigation-alternative/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

