<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">
  <channel>
    <title>Brandon Aaron</title>
    <link>http://brandonaaron.net/</link>
    <description>Mostly about jQuery</description>
    <language>en-us</language>
    <geo:lat>32.831228</geo:lat><geo:long>-97.145939</geo:long><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/BrandonAaron" type="application/rss+xml" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com" /><item>
      <title>Understanding the Context in jQuery</title>
      <description>&lt;p&gt;When selecting elements jQuery has an &lt;a href="http://docs.jquery.com/Core/jQuery#expressioncontext"&gt;optional second argument&lt;/a&gt; called context. This context provides a means to limit the search within a specific node. This is great when you have a very large &lt;span class="caps"&gt;DOM&lt;/span&gt; tree and need to find, for example, all the &lt;code&gt;&amp;lt;a&amp;gt;&lt;/code&gt; tags within a specific part of the &lt;span class="caps"&gt;DOM&lt;/span&gt; tree. I&amp;#8217;ve seen various articles, usually performance related, stating that you should be using a context for your selectors. While it is true that using a context for your selectors can potentially boost performance, most of these articles misuse the&amp;nbsp;context.&lt;/p&gt;

&lt;h3&gt;Finding the&amp;nbsp;Context&lt;/h3&gt;

&lt;p&gt;As of jQuery 1.3 the context is exposed as a property on the jQuery collection. Here is how you can check to see what the context&amp;nbsp;is.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('a').context; // =&amp;gt; document
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Running the above code shows that the context is the document element. In fact, the default context for all jQuery collections is the document in which jQuery was loaded. In other words, selectors are run against the whole document by&amp;nbsp;default.&lt;/p&gt;

&lt;h3&gt;Changing the&amp;nbsp;Context&lt;/h3&gt;

&lt;p&gt;The context needs to be a node to work properly. This is the part that is often overlooked. I&amp;#8217;ve overlooked it in the past as well. Lots of examples out there tell you to just pass a second selector to jQuery to act as the reference node for the search. While this seems to work, it is still running the search on the whole&amp;nbsp;document.&lt;/p&gt;

&lt;p&gt;Here is an example of passing a second selector as the context. By looking at the &lt;code&gt;context&lt;/code&gt; property you can see that the context is still the document&amp;nbsp;though.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('a', '#myContainer').context; // =&amp;gt; document
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;When jQuery encounters another selector for the context it actually converts it to say the following&amp;nbsp;instead.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('#myContainer').find('a');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This conversion also happens the same way if you pass a jQuery collection as the&amp;nbsp;context.&lt;/p&gt;

&lt;p&gt;Now lets actually look at how we can change the context for the jQuery&amp;nbsp;collection.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// get the node for the context
var context = $('#myContainer')[0];

// pass the context as the second argument
$('a', context).context; // =&amp;gt; &amp;lt;div id="myContainer"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From this example we can see that passing a node as the second argument actually changes the context for the jQuery&amp;nbsp;collection.&lt;/p&gt;

&lt;h3&gt;jQuery 1.3.3 and&amp;nbsp;.live()&lt;/h3&gt;

&lt;p&gt;In jQuery 1.3.x, the &lt;code&gt;.live()&lt;/code&gt; method binds its event handlers to the document. In the upcoming jQuery 1.3.3 release the &lt;code&gt;.live()&lt;/code&gt; method will bind its  events to the context of the jQuery collection. This means your &lt;code&gt;.live()&lt;/code&gt; events can be more focused and potentially&amp;nbsp;faster.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/if2FZIu6qno" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Wed, 24 Jun 2009 19:20:09 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/if2FZIu6qno/understanding-the-context-in-jquery</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/06/24/understanding-the-context-in-jquery</feedburner:origLink></item>
    <item>
      <title>Automating with Special Events</title>
      <description>&lt;p&gt;&lt;a href="http://davidwalsh.name/"&gt;David Walsh&lt;/a&gt; had a great idea to coerce an element that is given a click event to have a pointer for its cursor. The pointer is a universal symbol for users to know that something is clickable. Using this technique it would be &amp;#8220;automatic&amp;#8221; that any element given a click event would have a pointer. David set out to use the Special Event hooks within jQuery to make this work. Unfortunately for David those hooks are not widely documented, so his &lt;a href="http://davidwalsh.name/add-events-jquery"&gt;first attempt was unsuccessful&lt;/a&gt;. So, lets see how we can use the Special Event hooks to make this work as&amp;nbsp;desired.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you haven&amp;#8217;t done so, I recommend reading the &lt;a href="http://brandonaaron.net/blog/2009/03/26/special-events"&gt;Special Events&lt;/a&gt; blog post which explains what special events are and how to use them. In the upcoming jQuery 1.3.3 there will be &lt;a href="http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks"&gt;two more event hooks&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;The Special&amp;nbsp;Event&lt;/h3&gt;

&lt;p&gt;The code, once you understand the Special Event hooks, is fairly straight forward. When a click event is bound to an element, we want to make sure it has a pointer for its cursor. Here is what the code looks like to make this&amp;nbsp;work.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.click = {
    setup: function() {
        jQuery( this ).css( 'cursor', 'pointer' );
        return false;
    },

    teardown: fuction() {
        jQuery( this ).css( 'cursor', '' );
        return false;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So lets break this small piece of code down. There are two hooks, &lt;code&gt;setup&lt;/code&gt; and &lt;code&gt;teardown&lt;/code&gt;, that are called once per an element. In the &lt;code&gt;setup&lt;/code&gt; hook we add the &lt;span class="caps"&gt;CSS&lt;/span&gt; rule to make the cursor a pointer and in the &lt;code&gt;teardown&lt;/code&gt; hook we revert the change since it is no longer clickable. The &lt;code&gt;teardown&lt;/code&gt; hook is only called after the last click event is&amp;nbsp;unbound.&lt;/p&gt;

&lt;p&gt;The only difference here from David&amp;#8217;s example is the &lt;code&gt;return false&lt;/code&gt; statements in both hooks. The return value in Special Event hooks is particularly important (and easy to overlook). Special Events assume that you will most likely handle the actual binding of the event yourself. In other words, when you don&amp;#8217;t return false jQuery skips the actual native binding (&lt;code&gt;addEventListner&lt;/code&gt; or &lt;code&gt;attachEvent&lt;/code&gt;) of the event to the element. In this particular case we want jQuery to go ahead and run its native binding code so the event is actually registered with the browser. To do this we need to return false from our&amp;nbsp;hooks.&lt;/p&gt;

&lt;h3&gt;Usage&lt;/h3&gt;

&lt;p&gt;Using the same example from David&amp;#8217;s post, the actual usage stays the same. Just bind a click event handler as you normally would using either &lt;code&gt;.bind()&lt;/code&gt; or &lt;code&gt;.click()&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery( document ).ready(function( $ ) {
    $( '#click-me' ).click(function() {
        var red   = Math.floor( Math.random() * 256 ),
            green = Math.floor( Math.random() * 256 ),
            blue  = Math.floor( Math.random() * 256 ),
            color = 'rgb(' + red + ',' + green + ',' + blue + ')';
        $( this ).css( 'background', color );
    });
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see this in action &lt;a href="/examples/automating-with-special-events/1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Enhancement&lt;/h3&gt;

&lt;p&gt;There is one enhancement that I&amp;#8217;d like to recommend. Instead of adding the styles via JavaScript, lets just add a class name instead. Maybe the name could be &amp;#8220;clickable&amp;#8221;. Then we can add a rule in our &lt;span class="caps"&gt;CSS&lt;/span&gt; that says anything with the class of &amp;#8220;clickable&amp;#8221; should have a pointer. This would make it easier to style specific instances of a clickable element. For example, we could add a three pixel blue border to a clickable image. :)  So, the final special event with this enhancement would look like&amp;nbsp;this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.click = {
    setup: function() {
        jQuery( this ).addClass( 'clickable' );
        return false;
    },

    teardown: fuction() {
        jQuery( this ).removeClass( 'clickable' );
        return false;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And in our &lt;span class="caps"&gt;CSS&lt;/span&gt; we would need to add a global rule that adds the pointer to &lt;code&gt;.clickable&lt;/code&gt;&amp;nbsp;elements.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;.clickable { cursor: pointer; }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can specialize our &lt;code&gt;.clickable&lt;/code&gt; class to handle certain elements differently. &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;img.clickable { border: 3px solid blue; cursor: pointer; }
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Last&amp;nbsp;thought&amp;#8230;&lt;/h3&gt;

&lt;p&gt;I had one last thought about this technique. This should not be a replacement for using semantic markup or using progressive enhancement. Most of the time if something should be clickable it should probably be clickable and actually do something when JavaScript is not&amp;nbsp;available.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/6027R41bIa8" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Wed, 17 Jun 2009 15:44:26 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/6027R41bIa8/automating-with-special-events</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/06/17/automating-with-special-events</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/06/17/automating-with-special-events</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: New Special Event Hooks</title>
      <description>&lt;p&gt;In jQuery 1.3.3 there are two new special event hooks: &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;remove&lt;/code&gt;. These two hooks, unlike &lt;code&gt;setup&lt;/code&gt; and &lt;code&gt;teardown&lt;/code&gt;, are called for each event being bound. The &lt;code&gt;add&lt;/code&gt; hook receives the handler, data, and namespaces as arguments. The &lt;code&gt;remove&lt;/code&gt; hook receives the data and namespaces as arguments. The &lt;code&gt;add&lt;/code&gt; and &lt;code&gt;remove&lt;/code&gt; hooks enable the creation of more complex, even customizable,&amp;nbsp;events.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you haven&amp;#8217;t done so, I recommend reading the &lt;a href="http://brandonaaron.net/blog/2009/03/26/special-events"&gt;Special Events&lt;/a&gt; blog post which explains what special events are and how to use them. For the following example I assume you already understand the concept of special events in&amp;nbsp;jQuery.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;As an example I&amp;#8217;m going to build a new special event called &amp;#8220;multiclick&amp;#8221;. This event tracks clicks on an element and fires at a given threshold or number of clicks. The required number of clicks will be customizable per a handler. &lt;/p&gt;

&lt;p&gt;First, here is how you&amp;#8217;d use the multiclick&amp;nbsp;event.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('div')
    .bind("multiclick", { threshold: 5 }, function( event ) {
        alert( "Clicked 5 times" );
    })
    .bind("multiclick", { threshold: 3 }, function( event ) {
        alert( "Clicked 3 times" );
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now lets build it. The skeleton of the multiclick special event looks like&amp;nbsp;this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.multiclick = {
    add: function( handler, data, namespaces ) {
        // called for each bound handler
    },

    setup: function( data, namespaces ) {
        // called once per an element
    },

    remove: function( namespaces ) {
        // called for each bound handler
    },

    teardown: function( namespaces ) {
        // called once per an element
    },

    handler: function( event ) {

    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Behind the scenes we&amp;#8217;ll need to use a normal click event. We&amp;#8217;ll use the &lt;code&gt;setup&lt;/code&gt; hook to bind the initial click event to track the actual clicks. The &lt;code&gt;handler&lt;/code&gt; method will be used to properly trigger the multiclick events. Once all the multiclick events are removed, the &lt;code&gt;teardown&lt;/code&gt; hook will be triggered. We&amp;#8217;ll need to unbind the the click event we used to track the clicks in the &lt;code&gt;setup&lt;/code&gt; hook. Here is the updated multiclick&amp;nbsp;event.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.multiclick = {
    add: function( handler, data, namespaces ) {
        // called for each bound handler
    },

    setup: function( data, namespaces ) {
        jQuery( this ).bind( "click", jQuery.event.special.multiclick.handler );
    },

    remove: function( namespaces ) {
        // called for each bound handler
    },

    teardown: function( namespaces ) {
        jQuery( this ).unbind( "click", jQuery.event.special.multiclick.handler );
    },

    handler: function( event ) {
        // set correct event type
        event.type = "multiclick";
        // trigger multiclick handlers
        jQuery.event.handle.apply( this, arguments );
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Next we need to make sure the bound function to a multiclick event isn&amp;#8217;t fired until it reaches the number of clicks specified. We&amp;#8217;ll utilize the &lt;code&gt;add&lt;/code&gt; hook for this. The &lt;code&gt;add&lt;/code&gt; hook has the ability to return a function which will take the place of the provided handler. This is a powerful feature and allows us to do all sorts of fun things with special events. In the case of the multiclick event we are going to return a new function that calls the given handler after the specified number of clicks. Here is the &lt;code&gt;add&lt;/code&gt;&amp;nbsp;hook.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.multiclick = {
    add: function( handler, data, namespaces ) {
        // get the required number of clicks from data
        var threshold = data &amp;amp;&amp;amp; data.threshold || 1,
            // number of clicks
            clicks = 0;

        // return a new function that will become the handler
        return function( event ) {
            // increase number of clicks
            clicks += 1;
            if ( clicks === threshold ) {
                // required number of clicks reached, reset
                clicks = 0;
                // call the actual supplied handler
                handler.apply( this, arguments );
            }
        }
    },

    ...
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The &lt;code&gt;add&lt;/code&gt; hook makes use of closures to keep track of the required number of clicks (threshold), the actual number of clicks, and the handler to be called once the required number of clicks is reached. The jQuery event system keeps everything in tact so that when you call unbind with the same named function that you supplied to bind, it still unbinds it as&amp;nbsp;expected.&lt;/p&gt;

&lt;p&gt;It ends up that we don&amp;#8217;t actually need to use the &lt;code&gt;remove&lt;/code&gt; hook for this special event, so we&amp;#8217;ll just remove it. Thats it. The multiclick special event is done. You can see it in action &lt;a href="/examples/jquery-edge-new-special-event-hooks/1"&gt;here&lt;/a&gt; and the complete code&amp;nbsp;follows.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.multiclick = {
    add: function( handler, data, namespaces ) {
        // get the required number of clicks from data
        var threshold = data &amp;amp;&amp;amp; data.threshold || 1,
            // number of clicks
            clicks = 0;

        // return a new function that will become the handler
        return function( event ) {
            // increase number of clicks
            clicks += 1;
            if ( clicks === threshold ) {
                // required number of clicks reached, reset
                clicks = 0;
                // call the actual supplied handler
                handler.apply( this, arguments );
            }
        }
    },

    setup: function( data, namespaces ) {
        jQuery( this ).bind( "click", jQuery.event.special.multiclick.handler );
    },

    teardown: function( namespaces ) {
        jQuery( this ).unbind( "click", jQuery.event.special.multiclick.handler );
    },

    handler: function( event ) {
        // set correct event type
        event.type = "multiclick";
        // trigger multiclick handlers
        jQuery.event.handle.apply( this, arguments );
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;h3&gt;Thanks&lt;/h3&gt;

&lt;p&gt;Thanks to &lt;a href="http://blog.threedubmedia.com/"&gt;Mike Helgeson&lt;/a&gt; who helped evolve the special events &lt;span class="caps"&gt;API&lt;/span&gt; to include these two hooks. He also has a handful of special events that he has created: &lt;a href="http://plugins.jquery.com/project/drag"&gt;drag&lt;/a&gt;, &lt;a href="http://plugins.jquery.com/project/drop"&gt;drop&lt;/a&gt;, &lt;a href="http://plugins.jquery.com/project/hover"&gt;hover&lt;/a&gt;, and &lt;a href="http://plugins.jquery.com/project/wheel"&gt;wheel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;If you knew about the &lt;code&gt;specialAll&lt;/code&gt; event hook, it has been removed in favor of using these two extra&amp;nbsp;hooks.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/1hqcCJ7Gg40" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Thu, 04 Jun 2009 01:05:18 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/1hqcCJ7Gg40/jquery-edge-new-special-event-hooks</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/06/4/jquery-edge-new-special-event-hooks</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Simplified .hover()</title>
      <description>&lt;p&gt;In jQuery 1.3.3 the &lt;code&gt;.hover()&lt;/code&gt; method will optionally accept a single method instead of always requiring two methods. This allows you to contain your logic within one method instead of duplicating it among two. I don&amp;#8217;t know about you but I&amp;#8217;ve avoided using the &lt;code&gt;.hover()&lt;/code&gt; method in favor of just manually binding the mouseenter and mouseleave events so that I could use a single function to handle both events. So, lets look at a typical use-case for&amp;nbsp;hover.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('li')
    .hover(function(event) {
        $(this).addClass('test');
    }, function(event) {
        $(this).removeClass('test');
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now, this is how I&amp;#8217;d typically handle this without the use of &lt;code&gt;.hover()&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('li')
    .bind('mouseenter mouseleave', function(event) {
        $(this).toggleClass('test');
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now lets use the new functionality of &lt;code&gt;.hover()&lt;/code&gt; instead of manually binding the&amp;nbsp;events.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('li')
    .hover(function(event) {
        $(this).toggleClass('test');
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Sweet!&lt;/p&gt;

&lt;p&gt;Sometimes though you&amp;#8217;ll need to do more complex interactions than just toggling a class. In these cases you can simply check the event type that is passed to your handler. When the user mouses over, the event type sent to the hover function will be mouseenter. When the user mouses out, the event type sent to the hover function will be mouseleave. Here is an example checking the event&amp;nbsp;type.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$('li')
    .hover(function(event) {
        $(this)[ (event.type == 'mouseenter') ? 'add' : 'remove') + 'Class' ]('test');
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In the above example I&amp;#8217;m still just toggling the class on the element that was hovered over/out but doing so in a more verbose way to illustrate how you can check the event&amp;nbsp;type.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/WLamW7b6qZk" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Thu, 28 May 2009 16:56:46 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/WLamW7b6qZk/jquery-edge-simplified-hover</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/28/jquery-edge-simplified-hover</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/28/jquery-edge-simplified-hover</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Better Support for other Windows and Documents</title>
      <description>&lt;p&gt;jQuery in the past hasn&amp;#8217;t always played well with other windows and documents than the one it was loaded in. However, jQuery is moving in the right direction to help ease the pain of cross window/frame development. There have been several commits recently that make previously window/document dependent methods, well&amp;#8230; less dependent. So far &lt;code&gt;.bind()&lt;/code&gt;, &lt;code&gt;.css()&lt;/code&gt;, &lt;code&gt;.width()&lt;/code&gt;, and &lt;code&gt;.height()&lt;/code&gt; have all been updated to work with other windows and documents. &lt;/p&gt;

&lt;p&gt;So, how might you use this new found jQuery power? First, if you didn&amp;#8217;t already know jQuery has a method, called &lt;a href="http://docs.jquery.com/Traversing/contents" title="Docs for contents method"&gt;&lt;code&gt;.contents()&lt;/code&gt;&lt;/a&gt;, that when used on an iframe element returns the document of the iframe. &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Get a reference to the iframe document
var iframeDoc = $('iframe').contents().get(0);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we can bind an event to it, get the dimensions, get styles of various elements,&amp;nbsp;etc.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Get width of iframe document
$(iframeDoc).width();

// Get height of iframe document
$(iframeDoc).height();

// Bind event to iframe document
$(iframeDoc).bind('click', function( event ) {
    // do something
});

// Get the style of an element in the iframe
$('div', iframeDoc).css('backgroundColor');
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Simple as&amp;nbsp;that.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/gNN8tqLpQB8" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Thu, 14 May 2009 11:22:00 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/gNN8tqLpQB8/jquery-edge-better-support-for-other-windows-and-documents</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/14/jquery-edge-better-support-for-other-windows-and-documents</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/14/jquery-edge-better-support-for-other-windows-and-documents</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Bind with a Different "this"</title>
      <description>&lt;p&gt;Brand new to jQuery &lt;span class="caps"&gt;SVN&lt;/span&gt; is the oft-requested feature of providing a different value for the &amp;#8220;this&amp;#8221; object in an event callback. Previously jQuery would always send the element as the value of &amp;#8220;this&amp;#8221; in the callback. You can utilize this new feature by passing the object you&amp;#8217;d like to represent &amp;#8220;this&amp;#8221; in the callback as the last argument to either &lt;code&gt;.bind()&lt;/code&gt; or &lt;code&gt;.live()&lt;/code&gt;. Lets look at some example&amp;nbsp;code!&lt;/p&gt;

&lt;p&gt;The typical use-case for this functionality is that you&amp;#8217;ll have an object (or a class) and you want to call a method of that class when you click on a particular element. However, the &amp;#8220;this&amp;#8221; object inside the method will be set to the element the event happened on. Usually in these situations you&amp;#8217;d want the &amp;#8220;this&amp;#8221; object to represent the instance of your class, not the element. Here is an example that tries to illustrate this&amp;nbsp;use-case.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// Create a class
var MyClass = function() {
    // initialize the class
    this.clicked = false;
    this.element = null;
};
MyClass.prototype.click = function( event ) {
    // handle the click event on an element
    // the "this" object should represent the instance of
    // the class, not the element the event happened on.
    // the element can be retreived using the event.target
    // or event.currentTarget

    // set the clicked property to true
    this.clicked = true;

    // set the element property to the element that was clicked
    this.element = event.target;

    // cancel the event
    return false;
};

// Create an instance of MyClass
var myInstance = new MyClass();

// Bind an event that calls the click method of myInstance
// and make sure the "this" object is set to myInstance
$("li").live("click", myInstance.click, myInstance);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;As you can see from the example code the actual element the event happened on can still be retrieved via the &lt;code&gt;event.target&lt;/code&gt; or &lt;code&gt;event.currentTarget&lt;/code&gt; properties. Both of these properties are normalized/fixed to work cross-browser (as is the &lt;code&gt;event.relatedTarget&lt;/code&gt; property). In the example above I used the &lt;code&gt;live&lt;/code&gt; method of binding events but the same applies for the &lt;code&gt;bind&lt;/code&gt;&amp;nbsp;method.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/9D1LDcX8iTk" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Tue, 12 May 2009 10:50:00 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/9D1LDcX8iTk/jquery-edge-bind-with-a-different-this</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/12/jquery-edge-bind-with-a-different-this</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/12/jquery-edge-bind-with-a-different-this</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Live Events now with Data</title>
      <description>&lt;p&gt;In jQuery 1.3.3 the &lt;code&gt;.live()&lt;/code&gt; method will have the ability to pass data along just like you might do with &lt;code&gt;.bind()&lt;/code&gt;. The &lt;code&gt;.bind()&lt;/code&gt; (and now &lt;code&gt;.live()&lt;/code&gt;) method takes an optional second argument called &amp;#8220;data&amp;#8221;. The data can then be accessed within the event handler by using the &lt;code&gt;event.data&lt;/code&gt; property. Here is what the code would look like to use this&amp;nbsp;feature.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// The data
var eventConfig = {
    selectedClass: "selected"
};

$("li").live("click", eventConfig, function( event ) {
    // Retreive selectedClass from the event data
    var selectedClass = event.data.selectedClass;

    // Toggle the class amongst the siblings
    $(this).addClass( selectedClass )
        .siblings().removeClass( selectedClass );
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So in the previous example, trivial as it may be, each time you click an &amp;#8220;li&amp;#8221; element the selected class defined in the &lt;code&gt;eventConfig&lt;/code&gt; is assigned to the clicked element and removed from the&amp;nbsp;siblings.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/sgAUUpiWTJw" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Fri, 08 May 2009 10:22:00 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/sgAUUpiWTJw/jquery-edge-live-events-now-with-data</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/8/jquery-edge-live-events-now-with-data</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/8/jquery-edge-live-events-now-with-data</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Versatile .index()</title>
      <description>&lt;p&gt;Paul Irish and ajpiano &lt;em&gt;(via Google Groups)&lt;/em&gt; saw an opportunity to make the &lt;code&gt;.index()&lt;/code&gt; method work better for them. Currently the &lt;code&gt;.index()&lt;/code&gt; method looks for a given element within the jQuery collection. However, they wanted a quick way to get the index of the current element amongst its siblings. Here is the scenario they laid&amp;nbsp;out.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$("#sometable th img").click(function() {
    // Need to find out the index of the &amp;lt;th&amp;gt;

    // get the &amp;lt;th&amp;gt;
    var $th = $(this).parent();

    // find the index, two options with jQuery 1.3.2
    var index = $("#sometable th).index($th);
    var index = $th.parent().children().index($th);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Both options in 1.3.2 aren&amp;#8217;t horrible but it could be a little more straightforward. With the new functionality to be in 1.3.3, you could do the following&amp;nbsp;instead.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$("#sometable th img").click(function() {
    // find out index of img's parent &amp;lt;th&amp;gt; in its own &amp;lt;tr&amp;gt;
    var index = $(this).parent().index();
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By not passing anything to the &lt;code&gt;.index()&lt;/code&gt; method, it will look for the first matched element of the jQuery collection within its siblings. So in the case of the above example we first select the image, then grab its parent (which is the &amp;lt;th&amp;gt;), and finally call &lt;code&gt;.index()&lt;/code&gt; which looks at its siblings to find its index among the other &amp;lt;th&amp;gt;&amp;nbsp;elements.&lt;/p&gt;

&lt;p&gt;That isn&amp;#8217;t the only new functionality though. You can also supply a selector to indicate which elements it should search within. For example we could find the index of the clicked image among all the images like&amp;nbsp;this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$("#sometable th img").click(function() {
   // find the index of the image among all images
   var index = $(this).index("img"); 
});
&lt;/code&gt;&lt;/pre&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/7yQUOf7TOJg" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Thu, 07 May 2009 11:05:00 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/7yQUOf7TOJg/jquery-edge-versatile-index</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/7/jquery-edge-versatile-index</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/7/jquery-edge-versatile-index</feedburner:origLink></item>
    <item>
      <title>jQuery Edge: Enhanced .toggleClass()</title>
      <description>&lt;p&gt;In jQuery 1.3.3 the &lt;code&gt;.toggleClass()&lt;/code&gt; method will have a couple more modes of operation. Currently in jQuery 1.3.2 the &lt;code&gt;.toggleClass()&lt;/code&gt; method can only toggle one class name at a time. The new &lt;code&gt;.toggleClass()&lt;/code&gt; method will be able to toggle multiple class names and will also be able to toggle all the classes on or off. Here are the different ways you&amp;#8217;ll be able to use &lt;code&gt;.toggleClass()&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// With the following element
// &amp;lt;div class="a b c"&amp;gt;&amp;lt;/div&amp;gt;

// Toggle all classes
$('div').toggleClass();        // &amp;lt;div class="" /&amp;gt;
$('div').toggleClass();        // &amp;lt;div class="a b c" /&amp;gt;
$('div').toggleClass( false ); // &amp;lt;div class="" /&amp;gt;
$('div').toggleClass( true );  // &amp;lt;div class="a b c" /&amp;gt;

// Toggle multiple classes
$('div').toggleClass( "a b" );          // &amp;lt;div class="c" /&amp;gt;
$('div').toggleClass( "a c" );          // &amp;lt;div class="a" /&amp;gt;
$('div').toggleClass( "a b c", false ); // &amp;lt;div class="" /&amp;gt;
$('div').toggleClass( "a b c", true );  // &amp;lt;div class="a b c" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;By calling &lt;code&gt;.toggleClass()&lt;/code&gt; without passing any class names, it will toggle all the current classes on the element. To toggle multiple class names, just separate the class names with a space. The optional boolean can force the removal (&lt;code&gt;false&lt;/code&gt;) or force the addition (&lt;code&gt;true&lt;/code&gt;) of the class&amp;nbsp;names.&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/8QN3MZccXSU" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Wed, 06 May 2009 09:35:00 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/8QN3MZccXSU/jquery-edge-enhanced-toggleclass</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/05/6/jquery-edge-enhanced-toggleclass</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/05/6/jquery-edge-enhanced-toggleclass</feedburner:origLink></item>
    <item>
      <title>Special Events</title>
      <description>&lt;p&gt;jQuery, since 1.2.2, has had an &lt;span class="caps"&gt;API&lt;/span&gt; for &amp;#8220;special events&amp;#8221;. These events are special because they have the ability to do some extra work for specific events and even the option to bypass some of the internal jQuery event system. With these special events you can create custom events that require some setup work or you can completely overwrite the behavior of native&amp;nbsp;events.&lt;/p&gt;

&lt;p&gt;We use special events in jQuery to create the &lt;a href="http://dev.jquery.com/browser/tags/1.3.2/src/event.js#L453"&gt;&amp;#8220;mouseenter&amp;#8221; and &amp;#8220;mouseleave&amp;#8221; events&lt;/a&gt;. In addition to those two events we used it to make the &lt;a href="http://dev.jquery.com/browser/tags/1.3.2/src/event.js#L342"&gt;&amp;#8220;ready&amp;#8221; event&lt;/a&gt; and I use it to normalize the &amp;#8220;mousewheel&amp;#8221; event in the &lt;a href="http://github.com/brandonaaron/jquery-mousewheel/tree/master"&gt;mouse wheel plugin&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;A Special Event:&amp;nbsp;&amp;#8220;tripleclick&amp;#8221;&lt;/h3&gt;

&lt;p&gt;To illustrate the &lt;span class="caps"&gt;API&lt;/span&gt; I&amp;#8217;m going to create a new event type called &amp;#8220;tripleclick&amp;#8221;. To be fired it will require the user click the element three times. If we were to make this a typical jQuery plugin we would create &lt;code&gt;jQuery.fn.tripleclick&lt;/code&gt;. However, I want to be able to take advantage of the &lt;code&gt;bind&lt;/code&gt; syntax along with the other benefits like event normalization, data, and namespaces that the jQuery event system&amp;nbsp;provides.&lt;/p&gt;

&lt;p&gt;The first thing we need to do is create the special event. Each special event has a &lt;code&gt;setup&lt;/code&gt; and a &lt;code&gt;teardown&lt;/code&gt; method. The &lt;code&gt;setup&lt;/code&gt; method gets called when the event is bound and the &lt;code&gt;teardown&lt;/code&gt; method gets called when the event is unbound. &lt;/p&gt;

&lt;p&gt;It is important to note that these two methods only get called the first time an event of a particular type is bound/unbound per an element. This is because the jQuery event system actually only binds one handler per an event type per an element and manages the other bound handlers itself. In jQuery 1.3 there is a new special event type called &amp;#8220;Special All&amp;#8221; that operates for all handlers but has a slightly different behavior. However, that is for another&amp;nbsp;article.&lt;/p&gt;

&lt;p&gt;The skeleton of our &amp;#8220;tripleclick&amp;#8221; special event looks like&amp;nbsp;this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.tripleclick = {
    setup: function(data, namespaces) {
        var elem = this;
    },

    teardown: function(namespaces) {
        var elem = this;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Notice that the &lt;code&gt;setup&lt;/code&gt; event gets passed the data and namespaces that were used when binding the event. Also that the &lt;code&gt;teardown&lt;/code&gt; event gets passed the namespaces that were used when unbinding the event. Although, they are only marginally useful here since this is the data and namespaces associated with the very first event handler bound to a particular element. But you could use the data to configure the event for all the handlers of that type for an element. The scope, or the value of &lt;code&gt;this&lt;/code&gt;, for these two methods is the element the event is being bound&amp;nbsp;to.&lt;/p&gt;

&lt;p&gt;Behind the scenes we are actually going to utilize the native &amp;#8220;click&amp;#8221; event to keep track of the number of clicks on an element. We&amp;#8217;ll also need a handler that actually does the heavy work of keeping track. I&amp;#8217;m going to add a third method called &lt;code&gt;handler&lt;/code&gt; to the &lt;code&gt;tripleclick&lt;/code&gt; special event. To make the code a little more simple I&amp;#8217;m going to track the number of clicks on an element by using the &lt;a href="http://docs.jquery.com/Core/data"&gt;jQuery data&lt;/a&gt;&amp;nbsp;&lt;span class="caps"&gt;API.&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The updated &lt;code&gt;tripleclick&lt;/code&gt; special event looks like&amp;nbsp;this.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery.event.special.tripleclick = {
    setup: function(data, namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.bind('click', jQuery.event.special.tripleclick.handler);
    },

    teardown: function(namespaces) {
        var elem = this, $elem = jQuery(elem);
        $elem.unbind('click', jQuery.event.special.tripleclick.handler);
    },

    handler: function(event) {
        var elem = this, $elem = jQuery(elem), clicks = $elem.data('clicks') || 0;
        clicks += 1;
        if ( clicks === 3 ) {
            clicks = 0;
            // set event type to "tripleclick"
            event.type = "tripleclick";
            // let jQuery handle the triggering of "tripleclick" event handlers
            jQuery.event.handle.apply(this, arguments)
        }
        $elem.data('clicks', clicks);
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;To quickly break down the handler code. First we get the number of clicks via the data &lt;span class="caps"&gt;API&lt;/span&gt; and increment the number by 1. Then we check to see if it has been clicked 3 times. If so, we then need to reset the number of clicks and trigger the other event handlers as the comment indicates. Finally, we store the new value for the number clicks on the element via the data&amp;nbsp;&lt;span class="caps"&gt;API.&lt;/span&gt; &lt;/p&gt;

&lt;p&gt;The handler has to set the event type to &amp;#8220;tripleclick&amp;#8221; because behind the scenes we actually use a click event. jQuery uses the event type to know which handlers it should call and we want it to call the event handlers for our &amp;#8220;tripleclick&amp;#8221;&amp;nbsp;event.&lt;/p&gt;

&lt;h3&gt;The&amp;nbsp;Example&lt;/h3&gt;

&lt;p&gt;We can now use our special event just like we&amp;#8217;d use any other event via the &lt;code&gt;bind&lt;/code&gt; method. For example to bind a &amp;#8220;tripleclick&amp;#8221; event to all divs we&amp;#8217;d write the following&amp;nbsp;code.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;jQuery('div').bind('tripleclick', function(event) {
    alert('triple clicked');
});
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;You can see an example of this new special event in action &lt;a href="/examples/special-events/1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You could enhance this event by requiring all three clicks to be within a certain timeframe. You could achieve this by also storing the &lt;code&gt;event.timeStamp&lt;/code&gt; property of the previous click and comparing the distance between it and the latest&amp;nbsp;click.&lt;/p&gt;

&lt;h3&gt;The &lt;code&gt;return&lt;/code&gt;&amp;nbsp;Value&lt;/h3&gt;

&lt;p&gt;I mentioned in the beginning of this article that a special event had the ability to bypass some of the internal jQuery event system. The functionality that can be skipped is the actual binding of the event to the element using the &lt;code&gt;addEventListener&lt;/code&gt; or &lt;code&gt;attachEvent&lt;/code&gt; methods. This functionality is skipped based on the return value. Any value other than &lt;code&gt;false&lt;/code&gt; prevents jQuery from actually binding the event to the element. In other words if you add &lt;code&gt;return false&lt;/code&gt; to the &lt;code&gt;setup&lt;/code&gt; and &lt;code&gt;teardown&lt;/code&gt; methods of your special event, it will actually use the native &lt;span class="caps"&gt;DOM&lt;/span&gt; APIs to bind the event to the element. In the case for our &amp;#8220;tripleclick&amp;#8221; event we didn&amp;#8217;t want to actually bind the event to the element using the native &lt;span class="caps"&gt;DOM&lt;/span&gt; APIs so we didn&amp;#8217;t return anything (&lt;code&gt;undefined&lt;/code&gt;).&lt;/p&gt;&lt;img src="http://feeds.feedburner.com/~r/BrandonAaron/~4/IdNb9olLQY8" height="1" width="1"/&gt;</description>
      <author>Brandon Aaron</author>
      <pubDate>Thu, 26 Mar 2009 01:57:10 +0000</pubDate>
      <link>http://feedproxy.google.com/~r/BrandonAaron/~3/IdNb9olLQY8/special-events</link>
      <guid isPermaLink="false">http://brandonaaron.net/blog/2009/03/26/special-events</guid>
    <feedburner:origLink>http://brandonaaron.net/blog/2009/03/26/special-events</feedburner:origLink></item>
  </channel>
</rss>
