<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>LA.NET [EN] : JQuery</title><link>http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx</link><description>Tags: JQuery</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>JQuery: full control with the $.ajax function</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-full-control-with-the-ajax-function.aspx</link><pubDate>Wed, 19 Aug 2009 14:17:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716650</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1716650</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-full-control-with-the-ajax-function.aspx#comments</comments><description>&lt;p&gt;In the previous posts, we’ve met several helper methods which simplify the code needed to perform remote out-of-band requests. In this post, we’ll be introducing the all mighty $.ajax method. This method is used by all the other methods we’ve seen until now and gives you complete control over the request. &lt;/p&gt;  &lt;p&gt;Currently, you’re supposed to use an anonymous object to pass all those &lt;a href="http://docs.jquery.com/Ajax/jQuery.ajax#options"&gt;definitions&lt;/a&gt; (which are really a lot!). To illustrate the use of this method, we’ll be consuming a ASP.NET web service which returns JSON. We’ll start with the service code (defined on the SayHiService.asmx(.cs) file):&lt;/p&gt;  &lt;pre class="code"&gt;[&lt;span style="color:#2b91af;"&gt;WebService&lt;/span&gt;(Namespace = &lt;span style="color:#a31515;"&gt;&amp;quot;http://tempuri.org/&amp;quot;&lt;/span&gt;)]
[&lt;span style="color:#2b91af;"&gt;WebServiceBinding&lt;/span&gt;(ConformsTo = &lt;span style="color:#2b91af;"&gt;WsiProfiles&lt;/span&gt;.BasicProfile1_1)]
[System.ComponentModel.&lt;span style="color:#2b91af;"&gt;ToolboxItem&lt;/span&gt;(&lt;span style="color:blue;"&gt;false&lt;/span&gt;)]
[System.Web.Script.Services.&lt;span style="color:#2b91af;"&gt;ScriptService&lt;/span&gt;]    
&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;SayHiService &lt;/span&gt;: System.Web.Services.&lt;span style="color:#2b91af;"&gt;WebService &lt;/span&gt;{
    [&lt;span style="color:#2b91af;"&gt;WebMethod&lt;/span&gt;]
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;User &lt;/span&gt;SayHello(&lt;span style="color:#2b91af;"&gt;String &lt;/span&gt;name) {
        &lt;span style="color:blue;"&gt;return new &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;User &lt;/span&gt;{
            Name = name,
            Date = &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now
        };
    }
}
&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;User &lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;String &lt;/span&gt;Name { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }
    &lt;span style="color:blue;"&gt;public &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DateTime &lt;/span&gt;Date { &lt;span style="color:blue;"&gt;get&lt;/span&gt;; &lt;span style="color:blue;"&gt;set&lt;/span&gt;; }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;We’ve created a simple User class that will return a user and the current time at which the request was received on the server. If you’re into ASP.NET AJAX, then you’ll know that it uses &lt;a href="http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx"&gt;JavascriptSerializer&lt;/a&gt; to serialize the return value into JSON. In the client side, you’re expected to transform the returned JSON into an object by using the &lt;a href="http://msdn.microsoft.com/en-us/library/bb310857.aspx"&gt;Sys.SerializationJavaScriptSerializer&lt;/a&gt; JS class.&lt;/p&gt;

&lt;p&gt;If you’re consuming ASP.NET web services from client code, there are some aspects you should keep in mind:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the returned object is wrapped into&amp;#160; an anonymous object with a single property (called d). This means that you’ll always receive a JSON string which looks like “{ ‘d’: … }”;&lt;/li&gt;

  &lt;li&gt;DateTime fields are transformed into /Date(x)/, where x is the number of ms elapsed since Jan. 1, 1970 at midnight UTC. In practice, this means that you’ll have to recover x and transform it into a valid JS Date object;&lt;/li&gt;

  &lt;li&gt;don’t forget that the web service needs to receive a JSON payload too. This means that you need to set the content type and you do need to serialize (into JSON) the arguments you’re passing from the client. If the method doesn’t receive any parameters, you should send the ‘{}’ string or it won’t work;&lt;/li&gt;

  &lt;li&gt;by default, the web service will only respond to POST requests (if you want to open it up to GET requests, you need to use the &lt;a href="http://www.asp.net/Ajax/Documentation/Live/mref/T_System_Web_Script_Services_ScriptMethodAttribute.aspx"&gt;ScriptMethodAttribute&lt;/a&gt; on the web service definition)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you used the &lt;a href="http://msdn.microsoft.com/en-us/library/bb310857.aspx"&gt;Sys.SerializationJavaScriptSerializer&lt;/a&gt; JS class, you don’t have to worry with these details. However, you might not want to load the MS AJAX file just to do the parsing of the returned JSON (btw, I’ve used this approach in an previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/03/03/the-mvc-framework-returning-json.aspx"&gt;post&lt;/a&gt; about ASP.NET MVC and JSON). And that’s what we’ll do in this post: we’ll use JQuery and the JSON plugin (which you can download from &lt;a href="http://code.google.com/p/jquery-json/"&gt;here&lt;/a&gt;) to interact with the previous web service. Here’s the final code of this page:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Server&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(function() {
        $(&amp;quot;input[type=button]&amp;quot;).click(
                
                function(evt) {
                    $.ajax({
                        url: &amp;quot;SayHiService.asmx/SayHello&amp;quot;,
                        type: &amp;quot;post&amp;quot;,
                        data: $.toJSON({ name: $(&amp;quot;input[type=text]&amp;quot;).val() }),
                        contentType: &amp;quot;application/json; charset=utf-8&amp;quot;,
                        dataType: &amp;quot;json&amp;quot;,
                        success: function(data) {
                            var user = data.d;
                            //update date                            
                            user.Date = new Date( parseInt(user.Date.match( /\d+/ )[0] ) );
                            var html = &amp;quot;Nome: &amp;quot; + user.Name + &amp;quot;--- Date: &amp;quot; + user.Date;
                            $(&amp;quot;div&amp;quot;).html(html);
                        }
                    });
                });
    });
    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;Some observations about the previous code:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;when calling the web service, you need to indicate the method that you want to invoke. That’s why we passed “SayHiService.asmx/SayHello” to the url property;&lt;/li&gt;

  &lt;li&gt;since I didn’t change the default server configuration, we need to ensure that the request will be made through a POST request (notice the type property);&lt;/li&gt;

  &lt;li&gt;we’re using the $.toJSON utility function to serialize an object which contains the information we want to send back to the server;&lt;/li&gt;

  &lt;li&gt;by passing json to the dataType, we’ll be getting a JS object in our success calback method;&lt;/li&gt;

  &lt;li&gt;the callback method starts by recovering the returned object (remember that we need to access the d property of an anonymous object). Then I used the most simple regular expression possible to recover the x from within the /Date(xxxx)/ string returned from the server side (as a side note, I’m betting that there are better ways of doing this, but regular expressions aren’t really my thing…);&lt;/li&gt;

  &lt;li&gt;you probably should pass a function to the error property so that you cna be notified about errors that have occurred during the request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I guess there’s more to say about this method, but I think that the previous snippet covers what you’ll be using in 99% of the times. If you have the time, then do take a look at all the available &lt;a href="http://docs.jquery.com/Ajax/jQuery.ajax#options"&gt;options&lt;/a&gt; because there’s some interesting stuff going on there.&lt;/p&gt;

&lt;p&gt;And that’s it. Keep tuned for more on &lt;a href="http://jquery.com/"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716650" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: deferred script loading</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-deferred-script-loading.aspx</link><pubDate>Wed, 19 Aug 2009 11:18:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716623</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1716623</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-deferred-script-loading.aspx#comments</comments><description>&lt;p&gt;Deferred script loading is a technique which can be used to reduce the initial loading time of any page. The idea is to load only the basic JavaScript code that is necessary for the initial loading operations of the page and then perform additional downloads for features that the user *might* use after initial rendering.&lt;/p&gt; &lt;p&gt;This is a fairly known and common technique and you’ll even find a pattern called &lt;a href="http://ajaxpatterns.org/On-Demand_Javascript"&gt;On-demand JavaScript&lt;/a&gt; on the &lt;a href="http://ajaxpatterns.org/"&gt;AJAX patterns&lt;/a&gt; web site. &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; helps by introducing the utility $.getScript function. To illustrate its use, lets assume that we’ve got a button that won’t be *heavily* used and that relies on several JavaScript lines of code. This is a scenario where the user experience can be improved by using deferred script loading. &lt;/p&gt; &lt;p&gt;We’ll be simulating this by using a simple JS file (called hi.js) with the following code:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;function &lt;/span&gt;sayHi(name) {
    alert(name);
}&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;
&lt;p&gt;Since we’re facing lots of JS lines (please imagine it:)) and this code isn’t necessary for the initial load of the page, we’ve decided to run it only when needed, ie, only when someone clicks the say Hi button. Here’s the code we might ended up writing:&lt;/p&gt;&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Server&amp;quot; /&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=button]&amp;quot;&lt;/span&gt;).click(            
                &lt;span style="color:blue;"&gt;function&lt;/span&gt;(evt) {                        
                    &lt;span style="color:blue;"&gt;if&lt;/span&gt;( !window.sayHi ) {
                        evt.target.disabled = &lt;span style="color:maroon;"&gt;&amp;quot;disabled&amp;quot;&lt;/span&gt;;
                        $.getScript(&lt;span style="color:maroon;"&gt;&amp;quot;hi.js&amp;quot;&lt;/span&gt;, &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
                            sayHi($(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=text&amp;quot;&lt;/span&gt;).val());
                            evt.target.disabled = &lt;span style="color:maroon;"&gt;&amp;quot;&amp;quot;&lt;/span&gt;;
                        });
                    }
                    &lt;span style="color:blue;"&gt;else&lt;/span&gt;{
                        sayHi( $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=text&amp;quot;&lt;/span&gt;).val() );
                    }
                });
    });
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;pre&gt;&lt;/pre&gt;
&lt;p&gt;It might seem complicated, but it’s not. Since we’re loading an JS file, we need to make sure that we’ll only load it once (loading several times the same user file doesn’t really do much for improving the user experience, right?). The easiest way to do that is to check for the existence of the sayHi function (it will be defined after the successful load of the JS file). To ensure that the user won’t click the button twice before the file is loaded, we need to disable it and enable it when the JS code has been loaded and evaluated (which is perfect for the callback function we can pass to the $.getScript function).&lt;/p&gt;
&lt;p&gt;And that’s it. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716623" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: the $.getJson method</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-getjson-method.aspx</link><pubDate>Wed, 19 Aug 2009 10:44:09 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716619</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1716619</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-getjson-method.aspx#comments</comments><description>&lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-get-and-post-methods.aspx"&gt;post&lt;/a&gt;, we’ve met the $.get and $.post methods. At the time, we’ve seen how we could easily load a JSON payload returned from the server. If you know that you’ll be getting JSON and that the request will be an HTTP GET, then you can use the $.getJson utility function instead of the $.get function.&lt;/p&gt;  &lt;p&gt;To illustrate its use, we’re going to reuse the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-get-and-post-methods.aspx"&gt;previous&lt;/a&gt; example and we’ll only be updating the client code of the page:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=button]&amp;quot;&lt;/span&gt;).click( 
            &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){
                $.getJSON(&lt;span style="color:maroon;"&gt;&amp;quot;HowdyHandler.ashx&amp;quot;&lt;/span&gt;, 
                      { name: $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=text]&amp;quot;&lt;/span&gt;).val()},
                      &lt;span style="color:blue;"&gt;function&lt;/span&gt;(data){
                        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).html(&lt;span style="color:maroon;"&gt;&amp;quot;Hi &amp;lt;b&amp;gt;&amp;quot; &lt;/span&gt;+ data.name + &lt;span style="color:maroon;"&gt;&amp;quot;&amp;lt;/b&amp;gt;&amp;quot; &lt;/span&gt;);
                     } );
            });
    
    });
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As you can see, the only difference is that the $.getJSON function receives only three parameters (when compared with the code we had to receive JSON with the $.get function). This isn’t surprising because this method reuses the $.get method internally for doing its work. Here’s its current implementation:&lt;/p&gt;

&lt;pre class="code"&gt;getJSON: &lt;span style="color:blue;"&gt;function&lt;/span&gt;( url, data, callback ) {
    &lt;span style="color:blue;"&gt;return &lt;/span&gt;jQuery.get(url, data, callback, &lt;span style="color:maroon;"&gt;&amp;quot;json&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And that’s it for today. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716619" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: the $.get and $.post methods</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-get-and-post-methods.aspx</link><pubDate>Wed, 19 Aug 2009 10:27:23 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716610</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1716610</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/19/jquery-the-get-and-post-methods.aspx#comments</comments><description>&lt;p&gt;In this post we’ll keep looking at &lt;a href="http://jquery.com/"&gt;JQuery&lt;/a&gt;’s helper methods for remote calls and we’ll see how we can use the get and post functions for performing HTTP GET and POST requests. Both functions (notice that these are utility functions and not JQuery object methods!) expect several parameters:&lt;/p&gt;  &lt;p&gt;$.get( url, [data], [callback], [type] )   &lt;br /&gt;$.post( url, [data], [callback], [type] )&lt;/p&gt;  &lt;p&gt;The first three parameters are the same as the ones we’ve met in a previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/17/jquery-getting-started-with-ajax.aspx"&gt;post&lt;/a&gt; when we looked at the load method. type is new and lets you indicate the type of response returned from the server. It’s important to set this if you’re getting non HTML/text content from the server side. Currently, it supports the values “xml&amp;quot;, &amp;quot;html&amp;quot;, &amp;quot;script&amp;quot;, &amp;quot;json&amp;quot;, &amp;quot;jsonp&amp;quot; and &amp;quot;text&amp;quot;.&lt;/p&gt;  &lt;p&gt;To show you how easy it is to use this utility function, we’ll start by creating a new handler which expects to retrieve a name from the request and returns an HTML snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;HowdyHandler &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IHttpHandler &lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ProcessRequest(&lt;span style="color:#2b91af;"&gt;HttpContext &lt;/span&gt;context) {
        context.Response.ContentType = &lt;span style="color:#a31515;"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;;
        context.Response.Write(
                &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;Hi &amp;lt;b&amp;gt;{0}&amp;lt;/b&amp;gt;!&amp;quot;&lt;/span&gt;, context.Request[&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;]) );
    }
    &lt;span style="color:blue;"&gt;public bool &lt;/span&gt;IsReusable { &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return false&lt;/span&gt;; } }
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Nothing special here, right? Now, take a look at the following HTML snippet:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text&amp;quot; /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Server&amp;quot; /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=button]&amp;quot;&lt;/span&gt;).click( 
            &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){
                $.get(&lt;span style="color:maroon;"&gt;&amp;quot;HowdyHandler.ashx&amp;quot;&lt;/span&gt;, 
                      { name: $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=text]&amp;quot;&lt;/span&gt;).val()},
                      &lt;span style="color:blue;"&gt;function&lt;/span&gt;(data){
                        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).html(data);
                     });
            });
    
    });
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;We’ve got a textbox and a button which will fire the AJAX request. If you run the previous snippet, you’ll see that you’ll end up doing an HTTP GET request and that you’ll get the “Hi name” message in the div. &lt;/p&gt;

&lt;p&gt;Even though getting HTML from the server is handy, it’s not something that I’d do in a typical web app. In my opinion, the server side should only return info&amp;#160; and the HTML formatting should be done by client side code (ie, code running in the browser). In these scenarios, I’ll probably tend to use JSON for the response. Lets update our handler to do that:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public void &lt;/span&gt;ProcessRequest(&lt;span style="color:#2b91af;"&gt;HttpContext &lt;/span&gt;context) {
    context.Response.ContentType = &lt;span style="color:#a31515;"&gt;&amp;quot;application/json&amp;quot;&lt;/span&gt;;
    context.Response.Write(
            &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;{{ &amp;#39;name&amp;#39;: &amp;#39;{0}&amp;#39; }}&amp;quot;&lt;/span&gt;, context.Request[&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;]) );
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;And now, we need to update our $.get call so that it knows that the response will be in JSON:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=button]&amp;quot;&lt;/span&gt;).click( 
            &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){
                $.get(&lt;span style="color:maroon;"&gt;&amp;quot;HowdyHandler.ashx&amp;quot;&lt;/span&gt;, 
                      { name: $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=text]&amp;quot;&lt;/span&gt;).val()},
                      &lt;span style="color:blue;"&gt;function&lt;/span&gt;(data){
                        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).html(&lt;span style="color:maroon;"&gt;&amp;quot;Hi &amp;lt;b&amp;gt;&amp;quot; &lt;/span&gt;+ data.name + &lt;span style="color:maroon;"&gt;&amp;quot;&amp;lt;/b&amp;gt;&amp;quot; &lt;/span&gt;);
                     },
                     &lt;span style="color:maroon;"&gt;&amp;quot;json&amp;quot;&lt;/span&gt;);
            });
    
    });
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;With this approach, we build the HTML in the client side. Since we’ve said that the response will be in JSON (notice that last parameter of the $.get function), data will now reference a JS object built from the JSON that is returned from the server. &lt;/p&gt;

&lt;p&gt;The $.post utility function is really similar to the $.get function. The main difference is that you end up doing an HTTP POST request instead of a GET. And that’s it for this post. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716610" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: getting started with AJAX</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/17/jquery-getting-started-with-ajax.aspx</link><pubDate>Mon, 17 Aug 2009 13:34:59 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1716321</guid><dc:creator>luisabreu</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1716321</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/17/jquery-getting-started-with-ajax.aspx#comments</comments><description>&lt;p&gt;Today marks the beginning of using &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; for handling AJAX requests. This is going to be a short post and we’ll be talking about the load method. The load method is able to perform get or post requests to replace the contents of the elements contained in the wrapped set with the response returned from the server side. As you’ve probably guessed, this method will only be usable in certain (few!) scenarios, where you’ll be writing the returned contents directly into one or more HTML elements (in my opinion, returning HTML from the server is a bad practice and you should avoid it at all costs).&lt;/p&gt;  &lt;p&gt;To illustrate the use of the load method, we’ll start by creating a new ASP.NET handler (which I’ll be appropriately calling DumbHandler) which returns the current server time:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public class &lt;/span&gt;&lt;span style="color:#2b91af;"&gt;DumbHandler &lt;/span&gt;: &lt;span style="color:#2b91af;"&gt;IHttpHandler &lt;/span&gt;{
    &lt;span style="color:blue;"&gt;public void &lt;/span&gt;ProcessRequest(&lt;span style="color:#2b91af;"&gt;HttpContext &lt;/span&gt;context) {
        context.Response.ContentType = &lt;span style="color:#a31515;"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;;
        context.Response.Write(&lt;br /&gt;           &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;&amp;lt;p&amp;gt;&amp;lt;span&amp;gt;{0}&amp;lt;/span&amp;gt;&amp;lt;/p&amp;gt;&amp;quot;&lt;/span&gt;, &lt;span style="color:#2b91af;"&gt;DateTime&lt;/span&gt;.Now));
    }
    &lt;span style="color:blue;"&gt;public bool &lt;/span&gt;IsReusable { &lt;span style="color:blue;"&gt;get &lt;/span&gt;{ &lt;span style="color:blue;"&gt;return false&lt;/span&gt;; } }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Now that we’ve got the handler, we can concentrate on the client code. The load method expects until three parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the url of the “page” that will be requested; &lt;/li&gt;

  &lt;li&gt;an optional data parameter that might contain an anonymous object with name/value pairs (results in a post request) or a string of name/value pairs (means you’ll be using a get request); &lt;/li&gt;

  &lt;li&gt;an optional callback method which will be fired when the AJAX request ends and each of the wrapped elements is updated with the returned content. Notice that this will also be called when you get an error in the request. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With this info, lets get started and build our first client AJAX script:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;main&amp;quot;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is another paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Replace content&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;bt&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input[type=button]&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
            &lt;span style="color:blue;"&gt;var &lt;/span&gt;url = &lt;span style="color:maroon;"&gt;&amp;quot;DumbHandler.ashx&amp;quot;&lt;/span&gt;;
            $(&lt;span style="color:maroon;"&gt;&amp;quot;#main&amp;quot;&lt;/span&gt;).load(url);
        });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The idea is simple: we handle the click event by performing a remote request through the load method. Since we’re only passing the url, this means that we’ll be performing a GET request and that the returned contents will be “written” inside our p#main paragraph.&lt;/p&gt;

&lt;p&gt;Lets suppose that I need to pass additional info to the server. If we want the info to be sent through GET, then we need to pass that information through the data parameter as a string. Here’s how we’d do that:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;url = &lt;span style="color:maroon;"&gt;&amp;quot;DumbHandler.ashx&amp;quot;&lt;/span&gt;;
    $(&lt;span style="color:maroon;"&gt;&amp;quot;#main&amp;quot;&lt;/span&gt;).load(url, &lt;span style="color:maroon;"&gt;&amp;quot;user=Luis&amp;quot;&lt;/span&gt;);
});&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;Really simple, right? Ok, but what if we needed to get that info form form fields? In that case, the serialize method is the way to go. To illustrate this method, let’s start by adding a form to our HTML page:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;form&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;name&amp;quot;&amp;gt;&lt;/span&gt;Name:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;name&amp;quot; /&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;label &lt;/span&gt;&lt;span style="color:red;"&gt;for&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;age&amp;quot;&amp;gt;&lt;/span&gt;Age:&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;label&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;select &lt;/span&gt;&lt;span style="color:red;"&gt;name&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;age&amp;quot;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;option &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;1&amp;quot;&amp;gt;&lt;/span&gt;10-20&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;option&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;option &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;2&amp;quot;&amp;gt;&lt;/span&gt;21-30&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;option&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;option &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;3&amp;quot;&amp;gt;&lt;/span&gt;31-40&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;option&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;option &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;4&amp;quot;&amp;gt;&lt;/span&gt;41+&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;option&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;select&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;br &lt;/span&gt;&lt;span style="color:blue;"&gt;/&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;form&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;main&amp;quot;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is another paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Replace content&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;bt&amp;quot; /&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;And here’s the changes we need to make to get the serialized info from our form fields:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;url = &lt;span style="color:maroon;"&gt;&amp;quot;DumbHandler.ashx&amp;quot;&lt;/span&gt;;
    $(&lt;span style="color:maroon;"&gt;&amp;quot;#main&amp;quot;&lt;/span&gt;).load(url, $(&lt;span style="color:maroon;"&gt;&amp;quot;form&amp;quot;&lt;/span&gt;).serialize());
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you want, you can only load a subset of the returned HTML. To do that, you need to filter the returned HTML by using a special selector delimited by SPACE on the url string. To show how this works, lets update our handler so that it returns the info that is sent through the AJAX request:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;public void &lt;/span&gt;ProcessRequest(&lt;span style="color:#2b91af;"&gt;HttpContext &lt;/span&gt;context) {
    context.Response.ContentType = &lt;span style="color:#a31515;"&gt;&amp;quot;text/html&amp;quot;&lt;/span&gt;;
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;name = context.Request[&lt;span style="color:#a31515;"&gt;&amp;quot;name&amp;quot;&lt;/span&gt;];
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;age = context.Request[&lt;span style="color:#a31515;"&gt;&amp;quot;age&amp;quot;&lt;/span&gt;];
    context.Response.Write(
            &lt;span style="color:#2b91af;"&gt;String&lt;/span&gt;.Format(&lt;span style="color:#a31515;"&gt;&amp;quot;&amp;lt;p&amp;gt;Name:&amp;lt;span&amp;gt;{0}&amp;lt;/span&amp;gt; at {1} &amp;lt;/p&amp;gt;&amp;quot;&lt;/span&gt;, &lt;br /&gt;                           name, age));
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, we return an html snippet which looks like &amp;lt;p&amp;gt;Name: &amp;lt;span&amp;gt;XXXX&amp;lt;/span&amp;gt; at YYY&amp;lt;p&amp;gt;. Lets assume that we’re only interested in showing the span element. That can be easily achieved by using the following url:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;url = &lt;span style="color:maroon;"&gt;&amp;quot;DumbHandler.ashx span&amp;quot;&lt;/span&gt;;&lt;/pre&gt;

&lt;p&gt;Notice the space delimiting the url from the selector…&lt;/p&gt;

&lt;p&gt;As I’ve said, you can also pass a callback method. This method will receive three parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the response text returned from the current request; &lt;/li&gt;

  &lt;li&gt;the text status (success is passed when everything runs ok); &lt;/li&gt;

  &lt;li&gt;a reference to the current XMLHttpRequest object that was responsible for executing the server side call (this lets you get more info about possible errors). &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you’ve probably guessed, the this reference will point to the “current” updated element of the wrapped set (don’t forget that the method ends up being called after each element of the wrapped set has been updated with the info returned from the server).&lt;/p&gt;

&lt;p&gt;And I guess this concludes this first query on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; and AJAX. Stay tuned for more.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1716321" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: running custom animations</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/14/jquery-running-custom-animations.aspx</link><pubDate>Fri, 14 Aug 2009 13:45:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1715670</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1715670</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/14/jquery-running-custom-animations.aspx#comments</comments><description>&lt;p&gt;In &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/11/jquery-getting-started-with-effects.aspx"&gt;the&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/12/jquery-fading-elements.aspx"&gt;previous&lt;/a&gt; &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/13/jquery-sliding.aspx"&gt;posts&lt;/a&gt;, we’ve met several methods which run some predefined animations. Instead of trying to add methods for all possible animations, &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; has decided to introduce a helper function which you can reuse for animating several css properties (notice that you can only animate properties which expect numeric values). &lt;/p&gt;  &lt;p&gt;Currently, there are two “overloads” of the animate method:&lt;/p&gt;  &lt;p&gt;animate(props, duration, easing, callback)   &lt;br /&gt;animate(props, options)&lt;/p&gt;  &lt;p&gt;As you can see, there are a lot of things going on here:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;props is an anonymous object which indicate the css properties that should be animated and their final values. It’s possible to use em or % as units and you can even use relative animations by passing –= or += to decrement or increment the current value of the specified css property;&lt;/li&gt;    &lt;li&gt;duration is used for defining the duration of the animation. It’s an optional parameter and, in 1.3, if you set it to 0, &lt;a href="http://jquery.com/"&gt;JQuery&lt;/a&gt; will synchronously set the elements to their end state. Before you ask, the answer is yes, you can also use the magic strings we’ve met in the past (slow, normal and fast);&lt;/li&gt;    &lt;li&gt;easing indicates the name of a previously registered function for performing the easing. According to the docs, there are two predefined easing functions: “linear” and “swing”;&lt;/li&gt;    &lt;li&gt;callback references a callback function which will be called for each of the elements when the animation ends;&lt;/li&gt;    &lt;li&gt;options is used on the second “overload” and it expects an anonymous object for setting the animation. Currently, you can define values for duration, easing, complete, step (method reference which will be called for each step of the animation) and queue (boolean value which indicates if the animation should be queued or started immediately).&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;There are some properties which probably deserve additional info. Easing influences the processing of frames and relies on mathematical calculations. I’m not really into this stuff, so I’ll leave further investigation of it to you :)&lt;/p&gt;  &lt;p&gt;If you use the “overload” which uses the options object, then you’ll also be able to decide if animations should be run in parallel. By default, animations are run synchronously, that is, if you call animate several times, one animation will only start after the other has finished (don’t confuse calling animate several times with animating several properties at once in a single animate call). If you’d like to run an animation at the time of invocation, then you’ll need to set the options’ queue property to false.&lt;/p&gt;  &lt;p&gt;I’m sorry, but I’m not really into design and effects, so I guess that the sample code for this post we’ll have to be even dumber than the ones I’ve presented in the&amp;#160; past. Take a look at the following code which animates an existing paragraph:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;paragraph&amp;quot;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Move and grow&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;bt&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
            $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;position&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;relative&amp;quot;&lt;/span&gt;)
                  .css(&lt;span style="color:maroon;"&gt;&amp;quot;backgroundColor&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;green&amp;quot;&lt;/span&gt;)
                  .animate( {left:100,
                                    width: &lt;span style="color:maroon;"&gt;&amp;quot;-=20%&amp;quot;&lt;/span&gt;,
                                    height: 300
                                    },
                                    200);
                });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Clicking the button kicks off a series of custom animations. We start by moving the P element so that it stays 100px left of the original position. Then, we decrease its current height by 20% and set its height in 300px. The whole thing should run in 200 ms.&lt;/p&gt;

&lt;p&gt;I’ll change the previous example slightly in order to show you how you could kick several. Take a look at the following code:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
            $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;position&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;relative&amp;quot;&lt;/span&gt;)
                  .css(&lt;span style="color:maroon;"&gt;&amp;quot;backgroundColor&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;green&amp;quot;&lt;/span&gt;)
            &lt;span style="color:#006400;"&gt;//.animate({ left: 100 }, 100)
                  &lt;/span&gt;.animate({
                      left: 100,                     
                      width: 300
                  },
                    5000)
                .animate({
                        height: 100
                    },
                    {
                        duration: 5000,
                        queue: &lt;span style="color:blue;"&gt;false
                    &lt;/span&gt;});
        });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Try changing the value of queue property and see what happens. Since I’ve set the animation to 5 seconds, it should be enough for understanding what’s going on.&lt;/p&gt;

&lt;p&gt;And that’s it! Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1715670" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: sliding</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/13/jquery-sliding.aspx</link><pubDate>Thu, 13 Aug 2009 08:59:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1715288</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1715288</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/13/jquery-sliding.aspx#comments</comments><description>&lt;p&gt;In this post, we’ll take a look at the &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; methods used for sliding animations. We’ve got 3 methods for executing sliding animations:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;slideDown: shows all the hidden elements contained in the wrapped set of the JQuery object.&lt;/li&gt;    &lt;li&gt;slideUp: hides all visible elements defined in the wrapped set.&lt;/li&gt;    &lt;li&gt;slideToggle: toggles the elements between hidden and visible.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;The methods expect the traditional speed (you can pass an integer or one of the “magic” strings we’ve met in the &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/11/jquery-getting-started-with-effects.aspx"&gt;past&lt;/a&gt;) and (optional) callback function parameters. If you didn’t notice, these methods are really similar to the show/hide/toggle methods we’ve met in a previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/11/jquery-getting-started-with-effects.aspx"&gt;post&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;The slide methods affect several css properties. Besides changing the height, the latest version of &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; (1.3 when I wrote this) will also animate the vertical padding and vertical margins of the elements in order to ensure a smoother&amp;#160; transition between visible and hidden (notice that element will also be removed from the display at the end of the animation). Here’s a small sample that shows how easy it is to add sliding to an element:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;html &lt;/span&gt;&lt;span style="color:red;"&gt;xmlns&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;http://www.w3.org/1999/xhtml&amp;quot; &amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;head &lt;/span&gt;&lt;span style="color:red;"&gt;runat&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;title&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;style &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/css&amp;quot;&amp;gt;
        &lt;/span&gt;p{ 
            border: solid 1px red;
            height: 100px;
            margin:10px;
        }           
    &lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;src&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;jquery-1.3.2.js&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;head&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is a paragraph&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;slide down/up&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(
            &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).slideToggle(500); });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;body&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;html&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;If you run the previous snippet, you’ll notice that clicking the button ends up sliding the p elements of the page (I’ve added a default height to all the paragraphs to make the transition more visible). If you’re interested in hiding the elements at page load, then you can simply set their CSS display property to none.&lt;/p&gt;

&lt;p&gt;And that’s it. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1715288" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: fading elements</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/12/jquery-fading-elements.aspx</link><pubDate>Wed, 12 Aug 2009 10:29:44 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1715049</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1715049</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/12/jquery-fading-elements.aspx#comments</comments><description>&lt;p&gt;In this post we’ll keep looking at the animations and we’ll see how to add fading effects to an element. Unlike showing and hiding, which affect several css properties of the element, fading will only affect the opacity (alpha filters if you’re running IE) and display of an element. Currently, &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; offers 3 methods: fadeIn, fadeOut and fadeTo. &lt;/p&gt;  &lt;p&gt;Here’s a small sample that shows how you can use the fadeIn and fadeOut methods:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;div2&amp;quot;&amp;gt;&lt;/span&gt;div 2&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;fade in/out&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).toggle(
                    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).fadeOut(300); },
                    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).fadeIn(300); }
                    );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you run the previous snippet, you’ll see that odd clicks change the element’s opacity in 300 ms and then it will be “removed” from the view (ie, its display will be set to none at the end of the animation). Fading in performs the opposite operation, changing the opacity from 0 to 1. Both methods can receive two parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the first indicates the speed&amp;#160; to run the animation. You can specify it in milliseconds (like we did in the previous snippet) or you can use one of the “magic” strings: slow, fast or def (stands for default). Notice that passing anything different from slow or fast is the same as passing def;&lt;/li&gt;

  &lt;li&gt;the second identifies a callback function which will be fired when the animation ends. You can use the this reference inside the function to get a reference to the DOM element that was animated.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you’ve set an initial opacity to the element by applying a style, then that value will be retained by these methods and fading in will always recover the element’s initial opacity. &lt;/p&gt;

&lt;p&gt;If you’ve run the previous sample, you’ve probably noticed that fading in and out result in setting the opacity to 1 and 0 respectively. In other words, those methods will always show or hide an element. If you need to go half way (ex.: change the opacity to 0.5), then you should use the fadeTo method. Let’s update the previous sample so that it fades an element between 0.2 and 0.8:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).toggle(
                    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).fadeTo(300, 0.2); },
                    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).fadeTo(300, 0.8); }
                    );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The fadeTo method expects until 3 parameters:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the first indicates the speed (it’s used in the same way as the speed parameter used in the fadeIn and fadeOut methods. Notice that you can also use the same “magic” strings we’ve mentioned above);&lt;/li&gt;

  &lt;li&gt;the second indicates the final value of the opacity and should have a value between 0 and 1;&lt;/li&gt;

  &lt;li&gt;finally, you can pass an (optional) callback method which will be fired when the animation completes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There’s a really important difference between fadeTo and the previous fade methods: fadeTo won’t do anything to the css display property of the element that is being animated.&lt;/p&gt;

&lt;p&gt;And that’s it. In the next post we’ll take a look at sliding. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1715049" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: getting started with effects</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/11/jquery-getting-started-with-effects.aspx</link><pubDate>Tue, 11 Aug 2009 10:00:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714863</guid><dc:creator>luisabreu</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714863</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/11/jquery-getting-started-with-effects.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; offers support for basic effects, like showing, hiding, fading and sliding. Besides that, it also facilitates the construction of new animations by introducing some custom methods which you can reuse to produce some basic animations. Today we’ll be concentrating on two methods: show and hide. Both these methods have two “overloads”: one takes no parameters and the other expects a timeout and an optional callback method (notice that Javascript doesn’t really support the OO overload concept since there’s only a single method which runs some tests on the passed arguments). &lt;/p&gt;  &lt;p&gt;Let’s start by looking at what happens when you call the methods and don’t pass any parameters. Calling hide is the same as setting the style’s display to none. Calling show results in setting the style’s display to the default element&amp;#39;s style or in recovering the previous default style (more about this in the next paragraphs). Take a look at the following snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;span 1&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;Another span&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;div2&amp;quot;&amp;gt;&lt;/span&gt;div 2&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {            
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).toggle(
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){$(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).hide();}, 
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;(){$(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).show();} );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you run the previous snippet, you’ll notice that the hide method effectively hides all the elements. Calling show ends up setting the style’s display property to the default value of each of the wrapped set elements. As I said before, if you had an initial value applied to the display property when you called hide, that value will be preserved and remembered for a future show call. When that happens, that initial style will be applied again. Take a look at the next snippet:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color:red;"&gt;display&lt;/span&gt;&lt;span style="color:blue;"&gt;: block&amp;quot;&amp;gt;&lt;/span&gt;span 1&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;Another span&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;div2&amp;quot;&amp;gt;&lt;/span&gt;div 2&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).toggle(
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).show(); },
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).hide(); } );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As you can see, we’re using our old toggle event function helper to alternate between functions for the click event. If you run this sample, you’ll notice that invoking hide still removes the wrapped set elements from the view (by setting the display property to none) and that when you call show, the span retains its initial display value. &lt;/p&gt;

&lt;p&gt;If you change the order of the calls (ie, if you change the order of the functions in the toggle method), you’ll end up loosing the initial display of the span. If you don’t believe me, then replace the previous script with the next:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).toggle(
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).show(); },
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).hide(); } );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;And now hiding/showing will always result in getting the default span’s display. &lt;/p&gt;

&lt;p&gt;You’re probably wondering what’s going on with hide and show. It’s not that complicated…in fact, it’s really simple. When you call hide, you end up saving the current display value to a named data slot that is added to each of the wrapped set elements (by using the internal &lt;a href="http://docs.jquery.com/Internals/jQuery.data#elemnamevalue"&gt;jQuery.data function&lt;/a&gt; – we’ll come back to this method in future posts). &lt;/p&gt;

&lt;p&gt;Whenever the show method is invoked, it starts by trying to get the previously stored display value from that named slot. If it finds one, it will apply it to the style’s display property. If it can’t find it, then it will simply create a new element of that type (ie, with the same tag name) so that it can get its default style’s display value (notice that this means that you end up adding/removing a new element to the body element to get that information). This is the default behavior you get when you don’t pass any parameters to these methods.&lt;/p&gt;

&lt;p&gt;If you decide to use the other “overloads” of these methods, you’ll really be adding animation effects. As I’ve said at the beginning of the post, the second “overload” of this method expects:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;a mandatory speed parameter, which defines the total time that will be taken to show/hide the element in milliseconds;&lt;/li&gt;

  &lt;li&gt;an optional callback function which will be called when the animation ends.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets update the previous snippet so that it uses these overloads:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot;&amp;gt;&lt;/span&gt;span 1&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;Another span&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;div2&amp;quot;&amp;gt;&lt;/span&gt;div 2&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).toggle(
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).hide(500); },
                        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).show(500); } );
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Running the previous code should result in an animation that changes the width, height, opacity, padding, margins and display of the element. This leads to a more graceful smoothing effect. Instead of specifying the speed in milliseconds, you can also use one of three predefined strings: slow, normal or fast.&lt;/p&gt;

&lt;p&gt;If you want, you can also pass a callback function to both the hide and show methods. The function doesn’t receive any parameters and you can access the “animated” DOM element by using the this reference. You should also keep in mind that most of the previous references made about the show and hide methods that take no parameters still hold: the only exception is that calling show on a visible element with a predefined display value doesn’t override that value with the elements default style’s display value.&lt;/p&gt;

&lt;p&gt;I know that I’ve said this post would only be about show and hide, but I guess there’s still time (and space) for introducing the toggle method. Take a look at the next snippet:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;style&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;&lt;/span&gt;&lt;span style="color:red;"&gt;display&lt;/span&gt;&lt;span style="color:blue;"&gt;: block&amp;quot;&amp;gt;&lt;/span&gt;span 1&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;Another span&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;span&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;class&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;test&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;div2&amp;quot;&amp;gt;&lt;/span&gt;div 2&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).toggle(); });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The previous snippet produces the same effect as our initial snippet. If you want, you can also pass values for speed and for a callback method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:maroon;"&gt;&amp;quot;.test&amp;quot;&lt;/span&gt;).toggle(&lt;span style="color:maroon;"&gt;&amp;quot;300&amp;quot;&lt;/span&gt;); });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, toggle can be used as click helper or as an animation helper that hides and then shows the wrapped set elements. The passed arguments decide which version will be used (notice that passing two functions results in getting the event method). &lt;/p&gt;

&lt;p&gt;And that’s it for today. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714863" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions X – extending objects</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-x-extending-objects.aspx</link><pubDate>Mon, 10 Aug 2009 14:06:34 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714705</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714705</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-x-extending-objects.aspx#comments</comments><description>&lt;p&gt;This is the last post of the day on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; and we’re going to talk about extending objects with the extend utility function. The idea is simple: how can you add properties from an object to another and get a new object? For instance, suppose you’ve got these two anonymous objects:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;student = { name: &lt;span style="color:maroon;"&gt;&amp;quot;Luis&amp;quot;&lt;/span&gt;, age: 20 };
&lt;span style="color:blue;"&gt;var &lt;/span&gt;address = { street: &lt;span style="color:maroon;"&gt;&amp;quot;some street&amp;quot;&lt;/span&gt;, civilParish: &lt;span style="color:maroon;"&gt;&amp;quot;somewhere&amp;quot;&lt;/span&gt;};&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;And now you’d like to add the address’ properties to the student object. You can do that by using the extend function:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;extendedStudent = $.extend(student, address);&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you’re using a debugger, you’ll notice that student has been “expanded” with the two properties defined on address:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/7345.extend_5F00_4CDCB5EE.jpg"&gt;&lt;img style="border-bottom:0px;border-left:0px;display:inline;border-top:0px;border-right:0px;" title="extend" border="0" alt="extend" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/6765.extend_5F00_thumb_5F00_0038794B.jpg" width="244" height="105" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;Notice that this method expects more parameters that the ones that we used in the previous snippet. If you pass several objects, you’ll end up adding the properties of all those objects to the first object. If some of the copied objects have properties with the same name, then the values of the last objects will override the initially copied values of the previous ones.&lt;/p&gt;

&lt;p&gt;There’s also a first optional boolean parameter which you can use to indicate if you’re doing a deep or a shallow copy of the properties of the objects. This parameter is optional and you’ll end up with a shallow copy when you don’t specify a value.&lt;/p&gt;

&lt;p&gt;And that it’s for today. I guess we’ve covered most of the current utility functions and now we’re ready to look at animations. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714705" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions IX – transforming arrays</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-ix-transforming-arrays.aspx</link><pubDate>Mon, 10 Aug 2009 13:51:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714701</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714701</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-ix-transforming-arrays.aspx#comments</comments><description>&lt;p&gt;The map utility function can be used to translate all the elements contained in the array into other set of items. You’ll find this method useful when you need to transform a collection of items A into items. The function expects two parameters:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;an array of items that is going to be translated; &lt;/li&gt;    &lt;li&gt;a callback function which will be called for each item and is responsible for transforming the existing item into a new item. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here’s a simple example which doubles all the integers contained on an array named arr:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [1, 2, 3, 4];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;doubled = $.map(arr, &lt;span style="color:blue;"&gt;function&lt;/span&gt;(item) { &lt;span style="color:blue;"&gt;return &lt;/span&gt;item * 2; });
alert(doubled);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You might be thinking that this is so simple that you could do it by using a simple for. Fair enough…and if I told you that I’d like to receive only the double of elements which aren’t divisible by four? Here’s how you’d do that:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [1, 2, 3, 4];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;doubled = $.map(arr, &lt;span style="color:blue;"&gt;function&lt;/span&gt;(item) { &lt;span style="color:blue;"&gt;if&lt;/span&gt;( item % 4 === 0 ) &lt;span style="color:blue;"&gt;return null&lt;/span&gt;; &lt;span style="color:blue;"&gt;return &lt;/span&gt;item * 2; });
alert(doubled);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Returning null removes an item from the final array. Ok, I can hear you thinking! Yes, this is still doable with a simple for and using continue for the cases that don’t matter…And what if I told you that I want the first three multiples of all numbers which aren’t divisible by four? (and I’m talking about a flat array, not an array of arrays). Here’s how you’d do it with this utility method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [1, 2, 3, 4];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;doubled = $.map(arr,
&lt;span style="color:blue;"&gt;function&lt;/span&gt;(item) {    
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;items = [];
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;i = 0;
    &lt;span style="color:blue;"&gt;while &lt;/span&gt;(i++ &amp;lt; 3) {
        items[i ] = item * i;
    }
    &lt;span style="color:blue;"&gt;return &lt;/span&gt;items;
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Simple and easy, right? For the sake of completeness, I’d like to point out that the callback function receives two arguments: the first (which was used in all the previous samples) references the current item of the array that is being enumerated; the second returns the current position within the array. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714701" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions VIII – creating arrays from other objects</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-viii-creating-arrays-from-other-objects.aspx</link><pubDate>Mon, 10 Aug 2009 13:30:39 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714695</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714695</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-viii-creating-arrays-from-other-objects.aspx#comments</comments><description>&lt;p&gt;The utility function makeArray is a helper which is used internally by &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; to transform collections of nodes into arrays. Even though this function is “public”, you’re not expected to use it much. Here’s its current internal implementation:&lt;/p&gt;  &lt;pre class="code"&gt;makeArray: &lt;span style="color:blue;"&gt;function&lt;/span&gt;( array ) {
    &lt;span style="color:blue;"&gt;var &lt;/span&gt;ret = [];
    &lt;span style="color:blue;"&gt;if&lt;/span&gt;( array != &lt;span style="color:blue;"&gt;null &lt;/span&gt;){
        &lt;span style="color:blue;"&gt;var &lt;/span&gt;i = array.length;
        &lt;span style="color:#006400;"&gt;// The window, strings (and functions) also have &amp;#39;length&amp;#39;
        &lt;/span&gt;&lt;span style="color:blue;"&gt;if&lt;/span&gt;( i == &lt;span style="color:blue;"&gt;null &lt;/span&gt;|| &lt;span style="color:blue;"&gt;typeof &lt;/span&gt;array === &lt;span style="color:maroon;"&gt;&amp;quot;string&amp;quot; &lt;/span&gt;|| jQuery.isFunction(array) || array.setInterval )
            ret[0] = array;
        &lt;span style="color:blue;"&gt;else
            while&lt;/span&gt;( i )
                ret[--i] = array[i ];
    }
    &lt;span style="color:blue;"&gt;return &lt;/span&gt;ret;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;As you can see, the code function starts by getting the length and checking the current type of the array parameter. If you’re passing a function (isFunction helper), a string, a window object or if the length doesn’t exist, you’ll end getting an array with a single element. &lt;/p&gt;

&lt;p&gt;In the remaining scenarios, you’ll go through all the elements in the array and you’ll copy them to an array. Notice that in Javascript it’s possible to have an object which isn’t an array but that does behave like one. Let me present a simple example of this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = (&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {&lt;br /&gt;   &lt;span style="color:blue;"&gt;return &lt;/span&gt;$.makeArray(arguments);
})(1, 2, 3);   &lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;So, what I’m doing here? Simple: I’m creating and executing an anonymous function and saving the returned result on the arr variable. The anonymous function will pass arguments to the makeArray function and you’ll get back a “real” array. If you debug the previous code, you’ll see that arguments behaves like an array but it’s not an array:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/8816.debugarguments_5F00_7F91E87E.jpg"&gt;&lt;img style="border-right-width:0px;display:inline;border-top-width:0px;border-bottom-width:0px;border-left-width:0px;" title="debugarguments" border="0" alt="debugarguments" src="http://msmvps.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/luisabreu.metablogapi/8883.debugarguments_5F00_thumb_5F00_6D38FBEF.jpg" width="244" height="151" /&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;As you can see, the arguments object has indexes and you even have a length property. However, things stop working if you try to use a method that exists on arrays. For instance, if you use the push method, you’ll end up getting an exception.&lt;/p&gt;

&lt;p&gt;The node lists which you get back from the methods like &lt;a href="http://www.w3schools.com/HTMLDOM/met_doc_getelementsbytagname.asp"&gt;getElementsByTagName&lt;/a&gt; behave like arrays but aren’t really arrays. In those cases, you can easily get an array by using this utility function. And that’s all for this post. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714695" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions VII – concatenating arrays</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-vii-concatenating-arrays.aspx</link><pubDate>Mon, 10 Aug 2009 10:23:25 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714668</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714668</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-vii-concatenating-arrays.aspx#comments</comments><description>&lt;p&gt;In this post we’re going to take a look at the merge utility function. This function expects two arrays that will be merged. The method returns a reference to the first array augmented with the elements contained on the second (which remains unchanged). Here’s a small snippet that shows how to use this function:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [0, 1, 2];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;anotherArr = [-1, -2, -3];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;merged = $.merge(arr, anotherArr);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The important thing here is that you shouldn’t forget that merged points to arr and that arr now has the items contained by anotherArr. Notice also that this function doesn’t remove duplicates. If you need that you’ll need to use the $.unique method (though it only works with DOM nodes). Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714668" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions VI – checking if an item is in an array</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-vi-checking-if-an-item-is-in-an-array.aspx</link><pubDate>Mon, 10 Aug 2009 09:34:49 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714659</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714659</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-vi-checking-if-an-item-is-in-an-array.aspx#comments</comments><description>&lt;p&gt;In the last &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/10/jquery-utility-functions-v-finding-elements-in-an-array.aspx"&gt;post&lt;/a&gt; we’ve seen how to get all the items that comply with a specific filter. In this post, we’re going to see how to see if an item is in an array. The easiest way to do that is to use the inArray function. Take a look at the following snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [0, -2, 4, 6, - 9];
&lt;span style="color:blue;"&gt;var &lt;/span&gt;positives = $.inArray(4,arr);&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The inArray method expects two parameters: the item that is going to be checked and the array that is going to be enumerated. Internally, the utility function relies on the the &lt;a href="http://www.w3schools.com/JS/js_comparisons.asp"&gt;=== operator&lt;/a&gt; for seeing if any of the array’s items matches the first parameter. &lt;/p&gt;

&lt;p&gt;Notice that inArray returns an integer. It will return&amp;#160; non negative integer that represents the position of the passed item. If it doesn’t find a matching item, it will simply return –1. And that’s it. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714659" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions IV – interacting through arrays</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-iv-interacting-through-arrays.aspx</link><pubDate>Fri, 07 Aug 2009 14:07:30 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714075</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714075</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-iv-interacting-through-arrays.aspx#comments</comments><description>&lt;p&gt;In this post, we’re going to look at the each utility function. This function expects two parameters: an object which is going to be enumerated and a callback function reference which is going to be called for each item in the enumerated set. It’s important to understand that this is different from the each jQuery method (which is used for interacting over each element of the wrapped set).&lt;/p&gt;  &lt;p&gt;Traditionally, you use indexes for going through all the items in an array. There are also some times where you need to enumerate all the properties of an object. In those cases, you’ll generally use the for in loop. If you want, you can reduce the code needed by using the each method. When you’re using an array, you’ll be writing a function that receives one parameter:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;arr = [1, 2, 3];
$.each(arr, &lt;span style="color:blue;"&gt;function&lt;/span&gt;(val) { alert(val); });&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;On the other hand, if you’re enumerating properties, then you’ll need (at least) two parameters: one for the property name and another for the value:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;obj = { name: &lt;span style="color:maroon;"&gt;&amp;quot;John&amp;quot;&lt;/span&gt;, age: 12 };
$.each(obj, &lt;span style="color:blue;"&gt;function&lt;/span&gt;(prop, val) { alert(prop + &lt;span style="color:maroon;"&gt;&amp;quot;:&amp;quot; &lt;/span&gt;+ val) });&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;You probably won’t find much value in using the functionality of this method, but&amp;#160; you’ll notice that it does reduce the amount of required typing. Before ending, there’s still time for saying that returning false from the method results in stopping the enumeration of the existing items.&lt;/p&gt;

&lt;p&gt;And that’s it. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714075" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions – testing helpers</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-testing-helpers.aspx</link><pubDate>Fri, 07 Aug 2009 13:32:17 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714070</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714070</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-testing-helpers.aspx#comments</comments><description>&lt;p&gt;One of the things you might need to do is check if a reference is an array or a function. &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; introduces two simple utility functions for performing these checks: the isArray and the isFunction methods. Both of them expect a single parameter which is used to perform the adequate test. Here’s a really dumb example that shows how to use the isFunction function:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;sayHi = &lt;span style="color:blue;"&gt;function&lt;/span&gt;(name) {
    alert(name);
}
&lt;span style="color:blue;"&gt;if &lt;/span&gt;($.isFunction(sayHi)) {
    sayHi(&lt;span style="color:maroon;"&gt;&amp;quot;Luis&amp;quot;&lt;/span&gt;);
}&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As you can see, we check the sayHi variable and we’ll only invoke it when it is, in fact, a function. These are really simple and rely on the typeof operator for checking the type of an existing reference. For instance, here’s the code for the previous isFunction helper:&lt;/p&gt;

&lt;pre class="code"&gt;isFunction: &lt;span style="color:blue;"&gt;function&lt;/span&gt;( obj ) {
    &lt;span style="color:blue;"&gt;return &lt;/span&gt;toString.call(obj) === &lt;span style="color:maroon;"&gt;&amp;quot;[object Function]&amp;quot;&lt;/span&gt;;
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And I guess that sums it up. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714070" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utility functions II – trimming strings</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-ii-trimming-strings.aspx</link><pubDate>Fri, 07 Aug 2009 13:21:02 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714064</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714064</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utility-functions-ii-trimming-strings.aspx#comments</comments><description>&lt;p&gt;In this post we’re going to talk about the helper trim method. This is really a simple function which removes any leading or trailing whitespaces from the passed string. Notice that the method uses regular expressions and considers white spaces as any char that matches spaces, form feed, new line, return, tab and vertical tab characters.&lt;/p&gt;  &lt;p&gt;Using the function is really simple, as&amp;#160; you can see from this snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;var &lt;/span&gt;someText = &lt;span style="color:maroon;"&gt;&amp;quot;    Hello there&amp;quot;&lt;/span&gt;;
alert(&lt;span style="color:maroon;"&gt;&amp;quot;-&amp;quot; &lt;/span&gt;+ $.trim(someText) + &lt;span style="color:maroon;"&gt;&amp;quot;-&amp;quot;&lt;/span&gt;);&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And that’s it. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714064" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: utilities functions I – browser detection</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utilities-functions-i-browser-detection.aspx</link><pubDate>Fri, 07 Aug 2009 10:54:15 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1714043</guid><dc:creator>luisabreu</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1714043</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/07/jquery-utilities-functions-i-browser-detection.aspx#comments</comments><description>&lt;p&gt;Today we’re going to start looking at the &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; utilities function. The utilities function include several low level helper methods used on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;’s core. The good news is that you can also reuse them in your own code. In this post we’re going to take a look at the browser detection helper functions.&lt;/p&gt;  &lt;p&gt;Before v1.3, &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; had several helper objects for getting info about the the browser where your pages were loaded. These objects were called browser, boxModel and styleFloat and they can (still) be accessed through properties on the jquery object ($.browser, $.boxModel and $.styleFloat). With v1.3, these old objects have been deprecated by a new property called support which contains most of the information you had in the previous properties. The advantage of the new support object is that it relies on feature detection instead of browser sniffering.&lt;/p&gt;  &lt;p&gt;Here is a quick preview of the properties you’ll find in the support object:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;boxModel: returns true if the page renders the box model according to the W3C Spec;&lt;/li&gt;    &lt;li&gt;cssFloat: true if style.cssFloat gives you access to the current CSS float value of an element;&lt;/li&gt;    &lt;li&gt;hrefNormalized: returns true when the browsed does not normalize href attribute values;&lt;/li&gt;    &lt;li&gt;htmlSerialize: true when the browser serializes link elements when innerHTML is used;&lt;/li&gt;    &lt;li&gt;leadingWhitespace: true if the browser preserves leading whitespace when innerHTML is used;&lt;/li&gt;    &lt;li&gt;noCloneEvent: true if the browser does not clone event handlers when elements are cloned;&lt;/li&gt;    &lt;li&gt;objectAll: equal to true when getElementsByTagName(“*”) returns all descendants;&lt;/li&gt;    &lt;li&gt;opacity: true if a browser can properly interpret the opacity style property (IE still relies on alpha filters);&lt;/li&gt;    &lt;li&gt;scriptEval: true when appendChild or createTextNode executes injected inline scripts;&lt;/li&gt;    &lt;li&gt;style: returns true only when getAttribute(“style”) returns the inline style specified on an element;&lt;/li&gt;    &lt;li&gt;tbody: true when browsers allow tables without tbody elements (IE adds the tbody element if you don’t explicitly add one to your HTML).&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;As you can see, many of these features won’t be used on day to day programming. However, if you’re a plug-in writer or if you intend to write Javascript code that isn’t browser specific, then these methods might help you in a couple of more uncommon scenarios.&lt;/p&gt;  &lt;p&gt;I guess that one of things that might have bitten you in the past is the box model. Currently, you can resume the problem to IE being able to render HTML element’s boxes in a standard way or in the quirks mode way (so that it is compatible with previous versions of the browser). With the W3C box model, the width/height defines the width/height of the content while IE’s quirk mode uses the width/height values for defining the total size of the element’s box. &lt;/p&gt;  &lt;p&gt;You’ll see no difference on sizes until you start using padding, margins or border widths. When you do that, you’ll notice that the in quirks mode the box will be fixed to the initial size specified through the width and height values). If you want to do your own calculations regarding the correct width and height and you need to make it work across several browsers, you can rely on this property for using the correct values.&lt;/p&gt;  &lt;p&gt;cssFloat might also be useful when you need to know how to get to the float style of a specific element. Take a look at the next example:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;left&amp;quot;&amp;gt;&lt;/span&gt;left&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;right&amp;quot;&amp;gt;&lt;/span&gt;right&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;change&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;Swap&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#change&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;(evt) {
            &lt;span style="color:blue;"&gt;var &lt;/span&gt;lft = $(&lt;span style="color:maroon;"&gt;&amp;quot;#left&amp;quot;&lt;/span&gt;)[0];
            &lt;span style="color:blue;"&gt;var &lt;/span&gt;rgt = $(&lt;span style="color:maroon;"&gt;&amp;quot;#right&amp;quot;&lt;/span&gt;)[0];
            &lt;span style="color:blue;"&gt;if &lt;/span&gt;($.support.cssFloat) {
                lft.style.cssFloat = &lt;span style="color:maroon;"&gt;&amp;quot;right&amp;quot;&lt;/span&gt;;
                rgt.style.cssFloat = &lt;span style="color:maroon;"&gt;&amp;quot;left&amp;quot;&lt;/span&gt;;
            }
            &lt;span style="color:blue;"&gt;else &lt;/span&gt;{
                lft.style.styleFloat = &lt;span style="color:maroon;"&gt;&amp;quot;right&amp;quot;&lt;/span&gt;;
                rgt.style.styleFloat = &lt;span style="color:maroon;"&gt;&amp;quot;left&amp;quot;&lt;/span&gt;;
            }
        });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The code shows how you can use the cssFloat property to change the initial floating applied to the element. Notice that I’m doing this only for demonstrating purposes because if this was a real app, I’d really use the css method (it would reduce the number of lines of code needed, which is always a good thing, right?).&lt;/p&gt;

&lt;p&gt;Getting the href url is generally done through the href property. However, we’ve already seen in a previous post that most properties get their values from an attribute bag. In most cases, properties can be seen as a shortcut. However, that’s not the case of the href property. When you access the href property, you’ll&amp;#160; always get the complete url. That might not happen if you access the value through the attribute bag. Take a look at the following snippet:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;a &lt;/span&gt;&lt;span style="color:red;"&gt;href&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;another.html&amp;quot;&amp;gt;&lt;/span&gt;Another page&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;a&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;input &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;button&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;id&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; &lt;/span&gt;&lt;span style="color:red;"&gt;value&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;show&amp;quot; /&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#show&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;(evt) {
            &lt;span style="color:blue;"&gt;var &lt;/span&gt;a = $(&lt;span style="color:maroon;"&gt;&amp;quot;a&amp;quot;&lt;/span&gt;)[0];
            alert($.support.hrefNormalized + &lt;span style="color:maroon;"&gt;&amp;quot; - &amp;quot; &lt;/span&gt;+ a.getAttribute(&lt;span style="color:maroon;"&gt;&amp;quot;href&amp;quot;&lt;/span&gt;));
        });
    });    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you run this code in a IE 7 or below, you’ll notice that the browser normalizes the url you’ve passed to the href attribute. That doesn’t happen when you’re using IE8 or any of the other main browsers (I haven’t tested it in safari).&lt;/p&gt;

&lt;p&gt;leadingWhitespace still produces different results when you compare IE with the other browsers. What this means is that IE8 still doesn’t respect leading whitespaces when you add content through the innerHTML property. Object cloning is also different in IE. The noCloneEvent property shows us that IE will clone event handlers when you clone an element (in other words, it returns false). That doesn’t happen in most of the other browsers. And the same thing happens for most of the remaining properties presented on the previous list (and that’s why I won’t go into details on all those properties). &lt;/p&gt;

&lt;p&gt;At the beginning of this post I said that the great advantage of the support object was that it relied on feature detection. In practice, this means that the code won’t use the traditional navigator object for getting information about the browser. If you look at JQuery’s code, you’ll notice that it creates several dumb elements in order to initialize the properties of the support object. This is great because the results returned from this detection process are way more accurate than the ones you’ll get with the navigator object (just remember that most browsers allow you to set a custom user agent string!). If you’re interested in seeing how it’s done,then you’ll have to scroll until line 3114 of the JQuery’s JS file. 
  &lt;br /&gt;&lt;/p&gt;

&lt;p&gt;And that’s it for today. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1714043" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: common “event pairs” helpers</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/05/jquery-common-event-pairs-helpers.aspx</link><pubDate>Wed, 05 Aug 2009 09:48:45 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1713452</guid><dc:creator>luisabreu</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1713452</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/05/jquery-common-event-pairs-helpers.aspx#comments</comments><description>&lt;p&gt;There are certain scenarios where you handle events in “pairs”. For instance, I guess we’ve all written JavaScript code for handling mouse over and mouse out events (which, btw, is also commonly known as hovering). Take a look at the following snippet:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&lt;/span&gt;Mouse over turns green! Mouse out turns black...
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).bind(&lt;span style="color:maroon;"&gt;&amp;quot;mouseover&amp;quot;&lt;/span&gt;, &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
           $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;green&amp;quot;&lt;/span&gt;);
        });
        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).bind(&lt;span style="color:maroon;"&gt;&amp;quot;mouseout&amp;quot;&lt;/span&gt;, &lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
           $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;black&amp;quot;&lt;/span&gt;);
        });
});    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If you run the previous sample, you’ll see that putting the mouse cursor over will result in changing the div’s text color to green and removing it will turn it back to black. &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; simplifies the code we need to write in these scenarios by introducing the hover method. Here’s how the code would look if we used that method:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).hover(
        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;green&amp;quot;&lt;/span&gt;); },
        &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;black&amp;quot;&lt;/span&gt;);});
});    
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The hover method expects two parameters: the first function will be invoked when the mouse enters the target and the second will be called when the cursor leaves that target.&lt;/p&gt;

&lt;p&gt;Besides this helper, there’s also the toggle method. This method receives two or more functions and will call each function one at a time for each click done over any of the container’s wrapped set elements. The next snippet keeps changing the text color of all divs for each click:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
    $(&lt;span style="color:maroon;"&gt;&amp;quot;div&amp;quot;&lt;/span&gt;).toggle(
    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;green&amp;quot;&lt;/span&gt;); },
    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;red&amp;quot;&lt;/span&gt;); },
    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;blue&amp;quot;&lt;/span&gt;); },
    &lt;span style="color:blue;"&gt;function&lt;/span&gt;() { $(&lt;span style="color:blue;"&gt;this&lt;/span&gt;).css(&lt;span style="color:maroon;"&gt;&amp;quot;color&amp;quot;&lt;/span&gt;, &lt;span style="color:maroon;"&gt;&amp;quot;black&amp;quot;&lt;/span&gt;);});
});    &lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Notice that there are no restrictions to the number of functions you can pass to the toggle method (and you’ll typically pass at least two when using this method.&lt;/p&gt;

&lt;p&gt; And that’s all for now. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1713452" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item><item><title>JQuery: more on the triggerHandler method</title><link>http://msmvps.com/blogs/luisabreu/archive/2009/08/05/jquery-more-on-the-triggerhandler-method.aspx</link><pubDate>Wed, 05 Aug 2009 09:14:10 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1713443</guid><dc:creator>luisabreu</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/luisabreu/rsscomments.aspx?PostID=1713443</wfw:commentRss><comments>http://msmvps.com/blogs/luisabreu/archive/2009/08/05/jquery-more-on-the-triggerhandler-method.aspx#comments</comments><description>&lt;p&gt;In the previous &lt;a href="http://msmvps.com/blogs/luisabreu/archive/2009/08/04/jquery-more-on-triggering-events.aspx"&gt;post&lt;/a&gt; I’ve shown you some code on how to use the triggerHandler method. At the time, I said that events would bubble even though the docs say that they won’t. I based my conclusions on a small simple test I’ve written. ingmar added more information on a comment saying that if you have a more complex tree, you won’t get the “correct” bubbling. For instance, with the following structure:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is some hidden content&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;And more hidden content&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;div&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;you’ll get the following bubbling: P-&amp;gt;BODY-&amp;gt;HTML. So, what’s happening here? Well, it’s simple: I was &lt;strong&gt;wrong&lt;/strong&gt;. The fact is that the bubbling I was seeing was coming from my button’s click event and not from the triggered element. Let’s take another peek at the script code I’ve written:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;script &lt;/span&gt;&lt;span style="color:red;"&gt;type&lt;/span&gt;&lt;span style="color:blue;"&gt;=&amp;quot;text/javascript&amp;quot;&amp;gt;
    &lt;/span&gt;$(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
        $(&lt;span style="color:maroon;"&gt;&amp;quot;*&amp;quot;&lt;/span&gt;).not(&lt;span style="color:maroon;"&gt;&amp;quot;input&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;(evt) {
            alert(evt.currentTarget.tagName + &lt;span style="color:maroon;"&gt;&amp;quot; clicked&amp;quot;&lt;/span&gt;);
            &lt;span style="color:blue;"&gt;return &lt;/span&gt;&lt;span style="color:maroon;"&gt;&amp;quot;hi&amp;quot;&lt;/span&gt;;
        });
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#trigger&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
            $(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).trigger(&lt;span style="color:maroon;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;);
        });
        $(&lt;span style="color:maroon;"&gt;&amp;quot;#triggerHandler&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;() {
            alert($(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).triggerHandler(&lt;span style="color:maroon;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;));
        });
    });
&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;script&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;As you can see, we’re hooking up all the elements’ click event (excluding inputs) with an anonymous function that displays an alert message with the tagName of the current target element. Now, when I wrote the small sample, I concentrated on the P’s click event and this was what I saw:&lt;/p&gt;

&lt;p&gt;P-&amp;gt;BODY-&amp;gt;HTML&lt;/p&gt;

&lt;p&gt;Since my initial markup looked like this:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;This is some hidden content&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;
&amp;lt;&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;And more hidden content&lt;span style="color:blue;"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color:maroon;"&gt;p&lt;/span&gt;&lt;span style="color:blue;"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;I completely forgot that the click event of the button would also propagate through the tree until it reaches the top element. Notice that when you dispatch an event from within an existing handler, that handler will be “suspended” until that “new” event is completely dispatched. When that happens, the “previous” event that was in “stand by” wakes up and goes through its normal lifecycle. So, if I had written this code:&lt;/p&gt;

&lt;pre class="code"&gt;$(&lt;span style="color:maroon;"&gt;&amp;quot;#triggerHandler&amp;quot;&lt;/span&gt;).click(&lt;span style="color:blue;"&gt;function&lt;/span&gt;(evt) {
    alert($(&lt;span style="color:maroon;"&gt;&amp;quot;p&amp;quot;&lt;/span&gt;).triggerHandler(&lt;span style="color:maroon;"&gt;&amp;quot;click&amp;quot;&lt;/span&gt;));
    evt.stopPropagation();
});&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;it would have been obvious that there was no bubbling coming from the P’s click event that has been dispatched through that anonymous function. I guess that the moral of the story is that I should pay more attention to the samples I’m writing during my &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt; tests. Thanks go to ingmar for putting me in the right track. Keep tuned for more on &lt;a href="http://jquery.com"&gt;JQuery&lt;/a&gt;.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1713443" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/luisabreu/archive/tags/JQuery/default.aspx">JQuery</category></item></channel></rss>