<?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>www.fooberry.com &#187; jQuery</title>
	<atom:link href="http://fooberry.com/category/jquery/feed/" rel="self" type="application/rss+xml" />
	<link>http://fooberry.com</link>
	<description>Sweetness Without Context</description>
	<lastBuildDate>Fri, 30 Jul 2010 14:17:02 +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>It&#8217;s all about the plug-ins</title>
		<link>http://fooberry.com/2010/03/24/its-all-about-the-plug-ins/</link>
		<comments>http://fooberry.com/2010/03/24/its-all-about-the-plug-ins/#comments</comments>
		<pubDate>Wed, 24 Mar 2010 21:23:59 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://fooberry.com/2010/03/24/its-all-about-the-plug-ins/</guid>
		<description><![CDATA[<br/>Let’s face it. jQuery has made writing JavaScript cool and fun. JavaScript had a bad reputation in my world for a long time, and for no really good reason. JavaScript is an amazing language. It’s the crappy compatibility browser implementation of that really sucks. Anyway, I’m digressing already. We write a lot of JavaScript to [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Let’s face it. jQuery has made writing JavaScript cool and fun. JavaScript had a bad reputation in my world for a long time, and for no really good reason. JavaScript is an amazing language. It’s the crappy compatibility browser implementation of that really sucks. Anyway, I’m digressing already.</p>
<p>We write a lot of JavaScript to make the UI snazzy, attractive, quick and easy to use. I’m sure we all write a lot of a JavaScript. I’m sure we all have tons of JS files sitting around and we include a handful of them on each page, mix and match, pick and choose. What a mess!</p>
<p>Besides the nightmare of having to know which files to include, on which pages, this method is also a performance drag. Each page needs to request 15 to 20 JS files. Sure the page browser is going to cache some of them, but we still pay for the expense in some way or another.</p>
<p>In addition to the performance problem, there is a deployment headache where we have to minify 30 different files. Sure we could script that, but still sounds like a headache.</p>
<p>So how do I have only one JS file (minus the core jQuery stuff I pull from a CDN)? Plug-ins! Every bit of JS I use on my pages is wrapped in a jQuery plug-in. Of course there is a tiny bit of JS that invokes the plug-in that is either directly on the page or in its own JS file, but in comparison of lines, it is statistically insignificant.</p>
<p>What this lets me do is combine every JS file I have into a single giant JS file, minify that, and link it once. Everything I could possibly do is pulled down in that one file, one time, via one request.</p>
<p>I’m sure we all have JS like the following.</p>
<pre class="javascript" name="code">$().ready(function(){
  $(&quot;.myButton&quot;).click(function(){
      alert(&quot;You clicked me.&quot;);
      $(&quot;.message&quot;)
           .css(&quot;color&quot;,&quot;blue&quot;)
           .text(&quot;my message is blue&quot;);
  });

});</pre>
<p>Pretty typical jQuery. Everyone likes blue messages. What I don&#8217;t like about this script is I can&#8217;t drop it on the page where I don&#8217;t want that to happen. If it&#8217;s in my master JS file, every page is going to get this action, and that&#8217;s probably not what I want. </p>
<p>So let&#8217;s make a plug-in for it. This is my basic pattern for creating a jQuery plug-in.</p>
<pre class="javascript" name="code">(function($){
  $.fn.messageForm = function(options){
    var defaults = {};
    var params = $.extend(options, defaults);    

    // do stuff here

    return this;
  }
})(jQuery);</pre>
<p>There are a lot of websites out there that can explain the pattern better than I can, but the basics are simple. </p>
<p>First, we create an anonymous function with a $ parameter and pass the jQuery object to it. This avoids any naming conflicts we might have with $ and ensures we can use it in our plug-in.</p>
<p>We setup some default values, although empty are at this point, and then merge them with the options that are passed into the call to the plug-in. This lets us easily parameterize the call without adding loads of new variables. </p>
<p>Lastly we return the jQuery object the plug-in was called on so we can allow chaining of other plug-ins.</p>
<p>Throwing in the custom code and we end up with this.</p>
<pre class="javascript" name="code">(function($){
  $.fn.messageForm = function(options){
    var defaults = {
          buttonSelector : &quot;.myButton&quot;,
          messageSelector : &quot;.message&quot;,
          messageColor : &quot;blue&quot;
        };
    var params = $.extend(options, defaults);
    var $this = this;

    function buttonClick(){
       alert(&quot;You clicked me.&quot;);
       $(params.messageSelector,$this)
         .css(&quot;color&quot;,params.messageColor)
         .text(&quot;my message is &quot; + params.messageColor);
    }   

    $(params.buttonSelector,$this).click(buttonClick);

    return this;
  }
})(jQuery);</pre>
<p>&#160;</p>
<p>What I can do now is put this JS in a file next to dozens of other similar plug-ins and serve it up. We still have to call it from somewhere though. </p>
<pre class="javascript" name="code">$().ready(function(){
   $(&quot;body&quot;).messageForm();
});</pre>
<p>I know the purists out there would scream, but I don’t mind tacking this on the bottom of my HTML. You can pass some parameters to it as well.</p>
<pre class="javascript" name="code">$().ready(function(){
   $(&quot;body&quot;).messageForm({messageColor:&quot;red&quot;});
});</pre>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2010/03/24/its-all-about-the-plug-ins/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Validation and ASP.Net Web Forms</title>
		<link>http://fooberry.com/2009/03/28/jquery-validation-and-aspnet-web-forms/</link>
		<comments>http://fooberry.com/2009/03/28/jquery-validation-and-aspnet-web-forms/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 15:37:26 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fooberry.com/2009/03/28/jquery-validation-and-aspnet-web-forms/</guid>
		<description><![CDATA[<br/>I&#8217;ve been dark for a while, but there has been a lot in the works. Hopefully there will be a flood of posts coming, most with a happier outcome than this one; however, to get back in the swing of things, I&#8217;ll take make it short and sweet. I get really frustrated with ASP.Net Web [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I&#8217;ve been dark for a while, but there has been a lot in the works. Hopefully there will be a flood of posts coming, most with a happier outcome than this one; however, to get back in the swing of things, I&#8217;ll take make it short and sweet.</p>
<p><strong>I get really frustrated with ASP.Net Web Forms</strong></p>
<p>We all know and love jQuery. It is an amazing JavaScript framework. It&#8217;s made it possible, for even the staunchest back-end developers, to get excited about writing UI. ASP.Net is the bane of jQuery enjoyment. I&#8217;ve covered this in <a title="www.fooberry.com » jQuery Selectors and ASP.Net Controls" href="/2009/01/07/jquery-selectors-and-aspnet-controls/">jQuery Selectors and ASP.Net Controls</a>. I have a new frustration: the giant HTML Form ASP.Net puts in the Web Form page.</p>
<p>There is an amazing jQuery <a title="bassistance.de » jQuery plugin: Validation" href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/">validation plugin</a>. It can do just about any validation you&#8217;ll need with minimal effort. If you want to validate a single form, it&#8217;s great; however, if you need to validate only a portion of the form, it&#8217;s a pain.</p>
<p>Since ASP.Net sticks its own <code>&lt;form&gt;</code> tag on the page by default, you&#8217;re pretty much screwed. The only option I&#8217;ve found so far is to go through each element, running a validation on the individual elements.</p>
<p>Here is an example of what I&#8217;m doing:</p>
<pre class="javascript">$().ready(function(){

	$("form").validate();	

	$("#firstname").rules("add", {
	 	minlength: 2,
		required:  true
	});

	$("#submit").click(function(){
		var valid = true;
		valid = valid &amp;&amp; $("form").validate().element("#firstname");
		alert(valid);
   	});
});</pre>
<p>I hope to find a better solution soon, but this will do for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/03/28/jquery-validation-and-aspnet-web-forms/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Animating Partial Postbacks and UpdatePanels with jQuery</title>
		<link>http://fooberry.com/2009/02/24/animating-partial-postbacks-and-updatepanels-with-jquery/</link>
		<comments>http://fooberry.com/2009/02/24/animating-partial-postbacks-and-updatepanels-with-jquery/#comments</comments>
		<pubDate>Wed, 25 Feb 2009 04:41:19 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Usability]]></category>
		<category><![CDATA[Visual Studio]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fooberry.com/?p=691</guid>
		<description><![CDATA[<br/>Update panels make it really easy to do pseudo ajaxish stuff with little or no extra effort. Wrap your controls in a an UpdatePanel, pay for the entire page to post back and go through its life-cycle and you&#8217;re pretty much there. I&#8217;ve already written a bit on the extra steps you&#8217;ll need if you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Update panels make it really easy to do <em>pseudo ajaxish</em> stuff with little or no extra effort. Wrap your controls in a an <code>UpdatePanel</code>, pay for the entire page to post back and go through its life-cycle and you&#8217;re pretty much there. I&#8217;ve already written a bit on the extra steps you&#8217;ll need if you&#8217;re doing some jQuery magic inside that <code>UpdatePanel</code>. It&#8217;s not difficult and easy enough to either delegate up the chain to something outside the <code>UpdatePanel </code>(e.g.<a href="http://fooberry.com/2009/01/03/jquery-and-event-delegation/"> event delegation</a>) or rinse and repeat the jQuery after the partial post back is complete (e.g. <code><a href="http://fooberry.com/2009/01/06/563/">add_endRequest</a></code>).</p>
<p>What would be really slick-o-matic is if we could do some animation to transition the partial postback so it wouldn&#8217;t just explode with new content in our face. Now granted, that may be an overstatement.  The content changes usually aren&#8217;t drastic or very distracting to but let&#8217;s add a bit of polish.</p>
<p>Keep in mind this is a first draft, done in about 30 minutes after an idea landed in my head at 3am. It still needs some polishing, but the major hurdles are taken care of. </p>
<p>For our example, we&#8217;ll have a simple Div that will be worked upon in a partial postback. Our goal will be to gently transition the display of that work to the user.  Something like this.<br />
                                    <embed width="440" height="420" type="application/x-shockwave-flash" src="http://v5.tinypic.com/player.swf?file=27y6dm0&#038;s=5" FlashVars="gig_lt=1235535494596&#038;gig_pt=1235535644676&#038;gig_g=1"><br /><font size="1"><a href="http://tinypic.com/player.php?v=27y6dm0&#038;s=5">Original Video</a> &#8211; More videos at <a href="http://tinypic.com">TinyPic</a></font><br />
                                <img style="visibility:hidden;width:0px;height:0px;" border=0 width=0 height=0 src="http://counters.gigya.com/wildfire/IMP/CXNID=2000002.0NXC/bT*xJmx*PTEyMzU1MzU*OTQ1OTYmcHQ9MTIzNTUzNTY*NDY3NiZwPTIzNDQ3MSZkPSZnPTEmdD*mbz*wZWQzMDVkMmQxOGU*Nzc*OWE*ZGU5OWRkYjNhNGE2ZA==.gif" /></p>
<p>We&#8217;ll start off with some basic styles to help illustrate the change. We&#8217;ll change the content of the Div and also the class.</p>
<pre name="code" class="css">
	#mydiv{ width:200px; border:solid 1px black; font-size:xx-large; }
	.myclass0 { height:200px; background-color:Blue; }
	.myclass1 { height:100px; background-color:Red; }
	.myclass2 { height:150px; background-color:Lime; }
	.myclass3 { height:50px; background-color:Purple; color:White; }
</pre>
<p>Nothing too exciting, but our partial postback will give us some new dimensions and style. It&#8217;ll get the point across.</p>
<p>Next we need some HTML.</p>
<pre name="code" class="html">
&lt;form id="form1" runat="server">
	&lt;asp:ScriptManager ID="ScriptManager1" runat="server">
	&lt;/asp:ScriptManager>
	&lt;div>
		Original time: <%= DateTime.Now.ToLongTimeString() %> 
		&lt;asp:UpdatePanel ID="UpdatePanel1" runat="server">
			&lt;ContentTemplate>
				&lt;asp:Button runat="server" Text="Button" ID="MyButton"
						OnClick="MyButtonClick"
					OnClientClick="return ClientClick(this,'#mydiv'); " />
				&lt;div id="mydiv" runat="server">
					&lt;asp:Label runat="server" Text="Label"
                                           ID="MyLabel">&lt;/asp:Label>
				&lt;/div>
			&lt;/ContentTemplate>
		&lt;/asp:UpdatePanel>
	&lt;/div>
&lt;/form>
</pre>
<p>The magic is going to live in the <code>ClientClick </code>event handler, but forget about that for now. The server side <code>OnClick </code>event will do some trivial work.</p>
<pre name="code" class="c#">
 protected void MyButtonClick(object sender, EventArgs e)
 {
	 MyLabel.Text = DateTime.Now.ToLongTimeString();
	 var c = "myclass" + DateTime.Now.Millisecond % 4;
	 mydiv.Attributes.Add("class",c);
 }
</pre>
<p>This could just as easily toggled a view from edit mode to read-only, expanded a detailed area, etc. Here, we are just picking one of four css classese to use on the div and also giving a label the current time. Like I said this isn&#8217;t the cool part. </p>
<p>The final steps are to do the following:</p>
<ul>
<li>Get the jQuery animation to happen on the click of the button.</li>
<li> Get the partial postback to wait until the animation is done.</li>
<li> Get the DOM to appear in the pre-animation state when it comes back from the partial postback.</li>
<li>Get the animation to happen once we have the new page ready to go.</li>
</ul>
<p>The function needs to know what needs to be animated upon. I&#8217;ve been using slide transitions, but the animation is something I think can very easily be abstracted out. We make some quick tests to see if we are in the process of animating or if the animation has been completed. I struggled with how to delay the button click&#8217;s post back from happening until the animation is complete and finally decided to just let it happen a second time, but to flag the second click to allow it to continue posting back. I&#8217;m using the jQuery data feature to keep some state about the button. </p>
<p>If we are currently sliding, then there is nothing left to do, and if we are done sliding then we can skip the other steps and continue the postback.</p>
<pre class="javascript" name="code">
if (d.data("isSliding")) return false;
if (d.data("doneSliding")) return true;
</pre>
<p>If we don&#8217;t meet those conditions, we need to slide. We set our flag, wire up the end request handler and the callback to the slide method and kick off the slide.  </p>
<p>The slide method is the simplest. </p>
<pre class="javascript" name="code">
d.slideUp(500,
	function() {
		d.data("isSliding", false);
		d.data("doneSliding", true);
		$(sender).click();
	});
</pre>
<p>Slide our control up, and when we&#8217;re done, set some flags and kick off the click a second time. With the flags set, we&#8217;ll fire off the postback. Nothing really fancy here either. Notice we can still reference <code>d</code> here. We don&#8217;t need to find it again. </p>
<p>The <code>endRequest </code>handler looks a bit more involved. </p>
<pre class="javascript" name="code">
var prm = Sys.WebForms.PageRequestManager.getInstance();

var f = function(s, e) {
	d = $(whatToSlideDown);
	d.css("display", "none");
	d.slideUp(0);
	d.slideDown(500);
	prm.remove_endRequest(f);
}

prm.add_endRequest(f);
</pre>
<p>Here, inside the function we do need to ask jQuery to find what we want again. The DOM has been changed and the old d we created from<code> $(whatToSlideUp) </code>probably isn&#8217;t there anymore. In addition, we need to set the element in the state we left the old element. In this case, we want the display to be set to &#8220;none&#8221;. This allows the <code>slideDown </code>to work just like we would expect following a <code>slideUp</code>.</p>
<p>This is part of the setup that I don&#8217;t like. I&#8217;ve tried quickly running a <code>slideUp(0)</code> on the the new elements, but I was seeing a flash of the default state before getting the slide up. This is something that I would like to factor out. The animation, including this default state should be a  parameter to the function.</p>
<p>Here is a look at the function as a whole. It&#8217;s a stupid function name I know, but that is easily changed. </p>
<pre class="javascript" name="code">
function ClientClick(sender,whatToSlideUp,whatToSlideDown) {

	if (!whatToSlideDown)
		whatToSlideDown = whatToSlideUp;

	var d = $(whatToSlideUp);

	if (d.data("isSliding")) return false;
	if (d.data("doneSliding")) return true;

	d.data("isSliding", true);

	var prm = Sys.WebForms.PageRequestManager.getInstance();

	var f = function(s, e) {
		d = $(whatToSlideDown);
		d.css("display", "none");
		d.slideUp(0);
		d.slideDown(500);
		prm.remove_endRequest(f);
	}

	prm.add_endRequest(f);

	d.slideUp(500,
		function() {
			d.data("isSliding", false);
			d.data("doneSliding", true);
			$(sender).click();
		});
	return false;
}
</pre>
<p>That&#8217;s it.  There is definitely room to polish this up and make it more flexible. I&#8217;ll post back when I get it up and running in a real application so it looks more useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/02/24/animating-partial-postbacks-and-updatepanels-with-jquery/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Scoping JavaScript with Module Pattern</title>
		<link>http://fooberry.com/2009/02/10/scoping-javascript-with-module-pattern/</link>
		<comments>http://fooberry.com/2009/02/10/scoping-javascript-with-module-pattern/#comments</comments>
		<pubDate>Wed, 11 Feb 2009 03:38:25 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fooberry.com/2009/02/10/scoping-javascript-with-module-pattern/</guid>
		<description><![CDATA[<br/>I&#8217;ve entered the world of actually writing JavaScript. In the past, usability was really a secondary concern. I need to do a postback to make a button visible, no problem. The screen will flash, but who cares. It&#8217;s not like the user is going to stop using my application to do the stack of work [...]]]></description>
			<content:encoded><![CDATA[<br/><p><a href="http://flickr.com/photos/darwinbell/538421096/in/set-72157594189670735"><img class="alignleft" title="Padlock" src="http://farm2.static.flickr.com/1010/538421096_5b98da6805_m.jpg" alt="" width="169" height="240" /></a>I&#8217;ve entered the world of actually writing JavaScript. In the past, usability was really a secondary concern. I need to do a postback to make a button visible, no problem. The screen will flash, but who cares. It&#8217;s not like the user is going to stop using my application to do the stack of work on their desk. They have to use it. It&#8217;s their only option.</p>
<p>Now that isn&#8217;t entirely true. We attempted to make it as usable as possible, but anything that even close to using jQuery was probably just, according to the powers that be, the developers just playing around with cool toys. I&#8217;m not sure the value of the user experience fell nicely into a quantifiable metric. I&#8217;m beginning to rant, so back to the point.</p>
<p>In my new adventures with jQuery, I&#8217;ve been writing a lot of JavaScript. The biggest complaint I&#8217;ve had with my JavaScript is that everything is global and can potentially be overwritten by subsequent JavaScript, or I might be overwriting someone else&#8217;s functions or variables. My JavaScript files looked like the following.</p>
<pre language="javascript" name="code">
var foo = "berry";
function doWork(s){
	alert(s+foo);
}

$(function(){doWork("foo")});
</pre>
<p>I just cross my fingers that no one else has a <code>doWork</code> function or <code>foo</code> variable. There has to be a way to scope this stuff. </p>
<p>Luckily, I found and article that explains exactly that. <a href="http://www.wait-till-i.com/2007/07/24/show-love-to-the-module-pattern/" title="Wait till I come!  &raquo; Blog Archive   &raquo; Show love to the Module Pattern">It explains the Module Pattern</a> and how to use it to limit the scope of JavaScript.</p>
<p>I&#8217;ll let you read the article for a more complete description that I could put together, but ultimately, my JavaScript ends up looking like this. </p>
<pre language="javascript" name="code">
myScript = function(){
  return {
	init:function(){doWork("foo");}
  }
  var foo = "berry";
  function doWork(s){
	alert(s+foo);
  }
}();
$(myScript.init);
</pre>
<p>In this case, <code>init</code> is the only method that gets exposed. You can&#8217;t invoke <code>doWork</code>. Also, by adding the scope of <code>myScript</code>, you can have greater confidence your methods and variables aren&#8217;t getting overwritten.</p>
<p>My practice thus far is to have one JavaScript file for each page and user control. The scope of each page&#8217;s file is the name of the page, or control. So you could see <code>Login.aspx.js</code> with a scope of <code>MyApp_Login</code>.</p>
<p>So far so good. We&#8217;ll see how it goes.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/02/10/scoping-javascript-with-module-pattern/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding parent elements with jQuery 1.3</title>
		<link>http://fooberry.com/2009/02/10/finding-parent-elements-with-jquery-13/</link>
		<comments>http://fooberry.com/2009/02/10/finding-parent-elements-with-jquery-13/#comments</comments>
		<pubDate>Tue, 10 Feb 2009 14:30:57 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fooberry.com/?p=644</guid>
		<description><![CDATA[<br/>I was recently writing a little javascript function that would traverse up the object graph and apply jQuery selectors to pick the correct parent that I needed. It was a pretty basic function, but I had that gut feeling that this is something that I shouldn&#8217;t be writing. It has to be already written by someome.  [...]]]></description>
			<content:encoded><![CDATA[<br/><p><a title="magnifying monkey man. by ohhector, on Flickr" href="http://www.flickr.com/photos/ohhector/508933636/"><br />
<img class="alignright" src="http://farm1.static.flickr.com/211/508933636_0ab1a791bb_m.jpg" alt="magnifying monkey man." width="240" height="160" /></a></p>
<p>I was recently writing a little javascript function that would traverse up the object graph and apply jQuery selectors to pick the correct parent that I needed. It was a pretty basic function, but I had that gut feeling that this is something that I shouldn&#8217;t be writing. It <em>has</em> to be already written by someome. </p>
<p>Luckily, with jQuery 1.3, it&#8217;s part of the core library. New in this version is the <code><a href="http://docs.jquery.com/Traversing/closest">closest</a></code> method which will find the closest parent that matches the selector you pass.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/02/10/finding-parent-elements-with-jquery-13/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.Net Ajax payload size compared to jQuery</title>
		<link>http://fooberry.com/2009/02/06/aspnet-ajax-payload-size-compared-to-jquery/</link>
		<comments>http://fooberry.com/2009/02/06/aspnet-ajax-payload-size-compared-to-jquery/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 14:32:53 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Chrome]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://fooberry.com/2009/02/06/aspnet-ajax-payload-size-compared-to-jquery/</guid>
		<description><![CDATA[<br/>Let me start off by saying this isn’t scientific, or possibly even accurate, but was pretty shocking at first glance. I wanted to take a look at what the true size of our pages were, so I brought up Chrome’s nice resource inspector and refreshed and average page from our intranet application. It was a [...]]]></description>
			<content:encoded><![CDATA[<br/><p>Let me start off by saying this isn’t scientific, or possibly even accurate, but was pretty shocking at first glance. I wanted to take a look at what the true size of our pages were, so I brought up Chrome’s nice resource inspector and refreshed and average page from our intranet application. It was a bit shocking at first.</p>
<p><img title="image" style="border-right: 0px; border-top: 0px; display: block; float: none; margin-left: auto; border-left: 0px; margin-right: auto; border-bottom: 0px" height="87" alt="image" src="http://fooberry.com/wp-content/uploads/2009/02/image-thumb.png" width="547" border="0" /></p>
<p> All that orange is javascript! Now, I know the browser is probably caching that, when it goes through IIS – Cassini won’t cache it—, butit still seems like a pretty big chunk, but it’s probably all the jQuery and jQuery plugins we’re using right?</p>
</p>
<p><a href="http://fooberry.com/wp-content/uploads/2009/02/image.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="51" alt="image" src="http://fooberry.com/wp-content/uploads/2009/02/image-thumb1.png" width="468" border="0" /></a> </p>
<p>Without the rest of the files, it’s hard to put that into perspective, but trust me ,that is pretty small. What about the plugins?</p>
<p><a href="http://fooberry.com/wp-content/uploads/2009/02/image1.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="308" alt="image" src="http://fooberry.com/wp-content/uploads/2009/02/image-thumb2.png" width="591" border="0" /></a> </p>
</p>
<p>Those are pretty small too. All that javascript has to be somewhere right? What about ASP.Net Ajax?</p>
<p><a href="http://fooberry.com/wp-content/uploads/2009/02/image2.png"><img title="image" style="border-right: 0px; border-top: 0px; display: inline; border-left: 0px; border-bottom: 0px" height="308" alt="image" src="http://fooberry.com/wp-content/uploads/2009/02/image-thumb3.png" width="640" border="0" /></a> </p>
<p>I had to scale the image so it would fit, but there is a lot there. You may have noticed that we are using the min version of the jQuery file. Is the ASP.Net Ajax minified?</p>
<p><strong>NOPE!</strong></p>
<p>I haven’t really looked to see if it is possible to shrink that JS somehow, we’ll see about doing that soon.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/02/06/aspnet-ajax-payload-size-compared-to-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery Selectors and ASP.Net Controls</title>
		<link>http://fooberry.com/2009/01/07/jquery-selectors-and-aspnet-controls/</link>
		<comments>http://fooberry.com/2009/01/07/jquery-selectors-and-aspnet-controls/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 15:50:46 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[ASP.Net]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://fooberry.com/2009/01/07/jquery-selectors-and-aspnet-controls/</guid>
		<description><![CDATA[<br/>As I’ve already mentioned, this is the bane of jQuery within an ASP.Net application. Selecting the control based on its ID becomes difficult with ASP.net Controls and even worse with naming containers. There have been a few methods I’ve used to find the ASP.Net control in the dom. Injecting the ClientID $(“#&#60;%= fooTextBox.ClientID %&#62;”) This [...]]]></description>
			<content:encoded><![CDATA[<br/><p>As I’ve already mentioned, this is the bane of jQuery within an ASP.Net application. Selecting the control based on its ID becomes difficult with ASP.net Controls and even worse with naming containers. There have been a few methods I’ve used to find the ASP.Net control in the dom.</p>
<h2>Injecting the ClientID</h2>
<pre language="javascript" name="code">$(“#&lt;%= fooTextBox.ClientID %&gt;”)</pre>
<p>This is my least favorite, but the most precise. There are no additional elements or attributes to add and you’re guaranteed to get the exact control you’re after. Another bad side effect is the javascript has to be inside the ASPX page. That’s not ideal either, although you could argue that having a small script that just has variables for the control names isn’t that bad.</p>
<pre language="javascript" name="code">var fooTextBoxClientID = “&lt;%= fooTextBox.ClientID %&gt;”;</pre>
<p>Still icky.</p>
<h2>Add Custom Attribute</h2>
<p>You could add a custom ID in the markup of the control. </p>
<pre language="html" name="code">&lt;asp:TextBox runat=”server” id=”fooTextBox” myID=”foo” /&gt;</pre>
<p>Then select on that custom ID.</p>
<pre language="javascript" name="code">$(“input[myID=’foo’]”)</pre>
<p>This seems to make the code less <a href="http://en.wikipedia.org/wiki/Don%27t_repeat_yourself">DRY</a>, but it works well. </p>
<h2>Find With Ending</h2>
<p>Since the name you give you .Net control is only prepended with its naming garbage, you can still find your names using the <em>ends with</em> selector.</p>
<pre language="javascript" name="code">$(“input[ID$=’fooTextBox’]")</pre>
<p>This works well when the controls are assured to appear only once of the form; however, when inside a repeating control such as a ListView or a reusable control, this method falls apart.</p>
<h2>Find With Class</h2>
<p>I probably use this method more than any other since most of the time I have a parent frame of reference which limits the scope enough that makes my class name unique. That scope limiter also helps with the other methods, but using class names has worked the best for me.</p>
<pre language="javascript" name="code">$(“.inputTextBox&quot;)</pre>
<p>Be careful not to use the CSS class like you would ID and create a CSS class for <em>every</em> element.</p>
<h2>Wrap in HTML Elements</h2>
<p>Also, you can wrap the control in an HTML element that will provide you full control over its ID.</p>
<pre language="html" name="code">&lt;span id=”fooText”&gt;&lt;asp:TextBox runat=”server” ID=”FooTextBox” /&gt; &lt;/span&gt;</pre>
<p>Then select it using the child / ancestor selector.</p>
<pre language="javascript" name="code">$(“#fooText &gt; input”)</pre>
<p>You’re adding unneeded markup here, but you might already have it and just need to add the ID attribute, or select it using the class name.</p>
<p>I hope this helps with some headaches surrounding ASP.Net and jQuery.</p>
<p><strong>Update:</strong> Since then, I&#8217;ve been using the following method more and more. The only change I&#8217;ve made is I&#8217;ve started to prefix my control name with the &#8220;_&#8221; to make sure I&#8217;m not picking up any control names that might carry similar ending names. </p>
<p>Like&#8230;</p>
<pre language="javascript" name="code">
$(“input[ID$=’_PhoneTextBox’]")
</pre>
<p>and&#8230;</p>
<pre language="javascript" name="code">
$(“input[ID$=’_CellPhoneTextBox’]")
</pre>
<p>Without the &#8220;_&#8221; both would match the following.</p>
<pre language="javascript" name="code">
$(“input[ID$=’PhoneTextBox’]")
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/01/07/jquery-selectors-and-aspnet-controls/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
		<item>
		<title>jQuery and UpdatePanels</title>
		<link>http://fooberry.com/2009/01/06/563/</link>
		<comments>http://fooberry.com/2009/01/06/563/#comments</comments>
		<pubDate>Tue, 06 Jan 2009 14:50:44 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[ASP.Net]]></category>

		<guid isPermaLink="false">http://fooberry.com/?p=563</guid>
		<description><![CDATA[<br/>While using a jQuery plugin to round corners, they started to disappear inside UpdatePanel&#8217;s after a partial post back. I found someone else on StackOverflow had the same problem. Luckily, the solution was right there as well. $(document).ready(function() { doReady(); var prm = Sys.WebForms.PageRequestManager.getInstance(); prm.add_endRequest(function(s, e) { doReady(); }); }); function doReady() { $('.roundedCorners').corners(); } [...]]]></description>
			<content:encoded><![CDATA[<br/><p>While using a jQuery plugin to round corners, they started to disappear inside UpdatePanel&#8217;s after a partial post back. I found <a href="http://stackoverflow.com/questions/256195/jquery-documentready-and-updatepanels">someone else on StackOverflow had the same problem</a>. Luckily, the solution was right there as well.</p>
<pre name="code" language="javascript">
$(document).ready(function() {
    doReady();

    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function(s, e) {
        doReady();
    });
});

function doReady() {
    $('.roundedCorners').corners();
}
</pre>
<p>I&#8217;m interested in trying the <a href="http://docs.jquery.com/Plugins/livequery">livequery jQuery plugin</a>, but will go with this for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/01/06/563/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>jQuery and Event Delegation Example</title>
		<link>http://fooberry.com/2009/01/04/jquery-and-event-delegation-example/</link>
		<comments>http://fooberry.com/2009/01/04/jquery-and-event-delegation-example/#comments</comments>
		<pubDate>Sun, 04 Jan 2009 16:08:47 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://fooberry.com/?p=557</guid>
		<description><![CDATA[<br/>I wanted to give some examples that actually run. The last post might have been a little abstract and brief. First, don&#8217;t forget to link in the jQuery script. &#60;script src="jquery-1.2.6.js" type="text/javascript" charset="utf-8">&#60;/script> Some CSS to pretty things up. .myContainer{ width:300px; font-family:Arial; } .fooberry{ margin:5px; } .summary{ background-color:blue; color:white; padding:3px; } .details{ background-color:green; color:white; } [...]]]></description>
			<content:encoded><![CDATA[<br/><p>I wanted to give some examples that actually run. The last post might have been a little abstract and brief.</p>
<p>First, don&#8217;t forget to link in the jQuery script.</p>
<pre language="html" name="code">
&lt;script src="jquery-1.2.6.js" type="text/javascript" charset="utf-8">&lt;/script>
</pre>
<p>Some CSS to pretty things up.</p>
<pre language="css" name="code">
	.myContainer{
		width:300px;
		font-family:Arial;
	}

	.fooberry{
		margin:5px;
	}
	.summary{
		background-color:blue;
		color:white;
		padding:3px;
	}

	.details{
		background-color:green;
		color:white;
	}

	.toolbar-details{
		cursor: pointer;
	}
</pre>
<p>Some HTML to manipulate.</p>
<pre name="code" language="html">
&lt;div class="myContainer">
	&lt;div class="fooberry">
		&lt;div class="summary">
			&lt;div class="summary-text">
				Just a little information.
			&lt;/div>
			&lt;div class="toolbar">
				&lt;span class="toolbar-details">details&lt;/span> |
				&lt;span>date: 1/3/2009&lt;/span>
			&lt;/div>
		&lt;/div>
		&lt;div class="details">
			More information....
		&lt;/div>
	&lt;/div>

	&lt;div class="fooberry">
		&lt;div class="summary">
			&lt;div class="summary-text">
				Different information.
			&lt;/div>
			&lt;div class="toolbar">
				&lt;span class="toolbar-details">details&lt;/span> |
				&lt;span>date: 1/4/2009&lt;/span>
			&lt;/div>
		&lt;/div>
		&lt;div class="details">
			More information....
		&lt;/div>
	&lt;/div>

&lt;/div>
</pre>
<p>Finally the jQuery.</p>
<pre name="code" language="javascript">
&lt;script>
	$(document).ready(function(){
		$(".fooberry").click(function(e){
			if($(e.target).is(".toolbar-details")){
				$(this).find(".details").slideToggle(500);
			}
		});
	});
&lt;/script>
</pre>
<p>This layout could probably be adjusted so the problems I mentioned in the previous post aren&#8217;t a problem, but let&#8217;s just look at the event delegation. </p>
<p>The div with the class &#8220;fooberry&#8221; contain the physical layout for the items of our list. We use jQuery to attach a click event handler to each &#8220;fooberry&#8221; div and then use jQuery again to examine the arguments of the event, and yet again to find the div we want to toggle. </p>
<p>I am sure it is possible to attach the event to the &#8220;myContainer&#8221; div. We would need some additional jQuery since the following line would find <em>all</em> &#8220;details&#8221; divs.</p>
<pre name="code" language="javascript">
$(this).find(".details").slideToggle(500);
</pre>
<p>This happens because the &#8220;this&#8221; in this statement will now be the &#8220;myContainer&#8221; div. We could probably write a query to find the parent of the &#8220;e.target&#8221; who&#8217;s class is &#8220;fooberry&#8221;, and then use that element in place of &#8220;this&#8221; in the previous statement. This would reduce the overhead of the redundant event handlers, and work with all additional &#8220;fooberry&#8221; divs added via Ajax.</p>
<p>I hope this is useful for you as it is for me.</p>
<p>Here is the entire HTML.</p>
<pre language="html" name="code">
	&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
	   "http://www.w3.org/TR/html4/strict.dtd">

	&lt;html lang="en">
	&lt;head>

		&lt;script src="jquery-1.2.6.js" type="text/javascript" charset="utf-8">&lt;/script>

		&lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		&lt;style>
			.myContainer{
				width:300px;
				font-family:Arial;
			}

			.fooberry{
				margin:5px;
			}
			.summary{
				background-color:#373999;
				color:white;
				padding:3px;
			}

			.details{
				background-color:#689980;
				color:white;
			}

			.toolbar-details{
				cursor: pointer;
			}
		&lt;/style>

		&lt;script>
			$(document).ready(function(){

				$(".fooberry").click(function(e){
					if($(e.target).is(".toolbar-details")){
						$(this).find(".details").slideToggle(500);
					}
				});
			});
		&lt;/script>

	&lt;/head>
	&lt;body>

		&lt;div class="myContainer">
			&lt;div class="fooberry">
				&lt;div class="summary">
					&lt;div class="summary-text">
						Just a little information.
					&lt;/div>
					&lt;div class="toolbar">
						&lt;span class="toolbar-details">details&lt;/span> |
						&lt;span>date: 1/3/2009&lt;/span>
					&lt;/div>
				&lt;/div>
				&lt;div class="details">
					More information....
				&lt;/div>
			&lt;/div>

			&lt;div class="fooberry">
				&lt;div class="summary">
					&lt;div class="summary-text">
						Different information.
					&lt;/div>
					&lt;div class="toolbar">
						&lt;span class="toolbar-details">details&lt;/span> |
						&lt;span>date: 1/4/2009&lt;/span>
					&lt;/div>
				&lt;/div>
				&lt;div class="details">
					More information....
				&lt;/div>
			&lt;/div>

		&lt;/div>

	&lt;/body>
	&lt;/html>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/01/04/jquery-and-event-delegation-example/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery and Event Delegation</title>
		<link>http://fooberry.com/2009/01/03/jquery-and-event-delegation/</link>
		<comments>http://fooberry.com/2009/01/03/jquery-and-event-delegation/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 18:25:23 +0000</pubDate>
		<dc:creator>mark</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Web Development]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://fooberry.com/?p=553</guid>
		<description><![CDATA[<br/>We&#8217;re currently implementing a collapsible panel in one of our web pages using the ASP.Net Ajax Collapsible Panel Extender. The problem we are having is the Collapsible Panel Extender requires Control ID&#8217;s for the expand, collapse and target controls. Usually this isn&#8217;t a problem, but we want to expand a panel inside a ListView control [...]]]></description>
			<content:encoded><![CDATA[<br/><p>We&#8217;re currently implementing a collapsible panel in one of our web pages using the ASP.Net Ajax Collapsible Panel Extender. The problem we are having is the Collapsible Panel Extender requires Control ID&#8217;s for the expand, collapse and target controls. </p>
<p>Usually this isn&#8217;t a problem, but we want to expand a panel inside a ListView control when a something is clicked outside the ListView. So now the problem is he only way to find the control ID is to do a complicated a string of <code>.Parent</code> and <code>.FindControl("")</code> calls to eventually call the <code>.ClientID</code> of the control we want. This becomes very brittle as the code breaks if the UI changes and some expects a parent to be there, when it&#8217;s been moved in he markup. This obviously needs to change.</p>
<p>So now to try it (the first way) in jQuery. It should be very easy right?</p>
<pre language="javascript" name="code">
	$("#" + buttonID).click(function(){
							$("#" + panelID).slideToggle(500);
						  });
</pre>
<p>Again, we have the same problem. The <code>buttonID</code> and <code>panelID</code> are not easy to get. We need that nasty <code>ClientID</code>; however, what dropping the ASP.Net controls in favor of old school HTML controls. We would end up with something like this?</p>
<pre language="javascript" name="code">
	$('#button&lt;%# Eval("myPK") %>').click( //...
</pre>
<p>Ugh&#8230;that will work, but it looks nasty too. Generating Javascript on the fly is cool and all I guess, but I don&#8217;t like it here.</p>
<p>Here comes Event Delegation to the rescue. Instead of attaching that <code>click</code> event handler to the actual item, let the event delegate up to a parent container, and catch it there. Then, using jQuery to only peer downward is elegant.</p>
<pre language="javascript" name="code">
	$(document).ready(function(){
		$(".outerDiv").click(function(e){
			if($(e.target).is(".collapseButton")){
				$(this).find(".collapsePanel").slideToggle(500);
			}
		})
	});
</pre>
<p>That&#8217;s it! That&#8217;s awesome! It works every time it appears on the page. Remember, we are inside a ListView so we repeat the need for this several times. We don&#8217;t care what is between the element with the &#8220;outerDiv&#8221; CSS class and the other elements because jQuery takes care of finding them. We&#8217;re no longer brittle!  We don&#8217;t need the control ID&#8217;s because we only care about the CSS classes now. No more Control ID&#8217;s! We no longer have difficulty finding out second cousin twice removed element because we found a common ancestor and look down from there. We don&#8217;t need to assign event handles for each button. Sweet! It is a win/win/win.</p>
]]></content:encoded>
			<wfw:commentRss>http://fooberry.com/2009/01/03/jquery-and-event-delegation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
