jQuery wrapped-set chaining

In this article we explore the JQuery chaining internal mechanism to see how it works and how it can improve our web development.

I The “wrapped set”

A) Definition

If you are a regular user of JQuery, you have probably heard about the “wrapped set” .In the official documentation the wrapped set is mentionned as simply a jQuery object.

The wrapped set is simply a list of DOM elements(with their children) in the order in which they are defined in the current document that matches a selector or in the order in which they have been created on the fly with the $(html) function.

Consequently, by definition, a wrapped set is a result of:

  • either a jQuery selector: $(‘.elementToBeSelected’)
  • or a DOM element created on the fly with jQuery: $(‘<div><span>Test</span></div>’). In this case the wrapped set only contains this single element and its children (if any)

To be exact, the wrapped set is a wrapper jQuery object and does not only contains those elements. It also offers numerous handy jQuery functions. Below is an example:

&lt;table border=&quot;0&quot; cellspacing=&quot;1&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Sexe&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;John&lt;/td&gt;&lt;td&gt;Male&lt;/td&gt;&lt;td&gt;35&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sue&lt;/td&gt;&lt;td&gt;Female&lt;/td&gt;&lt;td&gt;23&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sally&lt;/td&gt;&lt;td&gt;Female&lt;/td&gt;&lt;td&gt;28&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Peter&lt;/td&gt;&lt;td&gt;Male&lt;/td&gt;&lt;td&gt;25&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

The selector $(‘td:nth-child(1)’) will give the following wrapped set:

Please notice that the first 4 properties of the jQuery(‘td:nth-child(1)’) wrapped set is the 4 HTMLTableDataCellElement fetched by the selector (from 0 to 3). The following properties are jQuery functions (add, addClass, after…)

 

B) jQuery function classes

Indeed, all jQuery functions can be split into 2 distincts classes:

  • utility functions: functions that return a scalar value (string, number, void, …). These functions are of the form: jQuery.(args) or $.(args). Example:
    • $.trim(value): returns a trimmed string
    • $.each(container,callback): apply the passed called back function to the passed container. Not to be confused with $(selector).each(callback)
    • jQuery.parseJSON(jsonString): return a JSON object from an input String representation of the same object
  • wrapped set functions: functions that act on each element of the wrapped set and return a wrapped set. In this class we distinguish to sub-classes:
    • unmodifying wrapped-set functions: functions that do not modify the current wrapped set. The function is applied to each DOM element in the set and the original wrapped set is returned. Example: .addClass(cssClass), .attr(attribute[,value]), .each(callback)
    • modifying wrapped-set functions: these functions modify the current set to return a subset of its DOM elements. All these functions take a selector as unique argument. Example: .find(selector), .closest(selector), .filter(selector), .has(selector)

 
Let’s consider the following DOM portion:

&lt;table border=&quot;0&quot; cellspacing=&quot;1&quot;&gt;
    &lt;tr&gt;
      &lt;td class=&quot;centerText&quot;&gt;John&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sue&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Sally&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td&gt;Peter&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;

jQuery(‘td’) will return a wrapped set containing the following DOM elements:

      &lt;td class=&quot;centerText&quot;&gt;John&lt;/td&gt;
      &lt;td&gt;Sue&lt;/td&gt;
      &lt;td&gt;Sally&lt;/td&gt;
      &lt;td&gt;Peter&lt;/td&gt;

Now jQuery(‘td’).find(‘.centerText’) will filter the current set with all elements matching the selector. The new wrapped set is:

      &lt;td class=&quot;centerText&quot;&gt;John&lt;/td&gt;

 

II Chaining mechanism

A) Internal set stack

All function chainings in jQuery are based on non-utility functions. You can’t do chaining with utility functions simply because they only return scalar values and not wrapped set. Please notice that using an utility function in the middle of a chain will result in stopping it (makes sense).

The idea behing the function chaining is to navigate back and forth between different wrapped sets. Internally jQuery maintains a stack to keep track of all sets it encounters.

Let’s consider the following DOM section:

	&lt;div id=&quot;containterDiv&quot;&gt;
		&lt;div id=&quot;firstChild&quot;&gt;
			&lt;span class=&quot;centerText&quot;&gt;Test&lt;/span&gt;
		&lt;/div&gt;
		&lt;div id=&quot;secondChild&quot;&gt;
			&lt;span&gt;Another Text&lt;/span&gt;
		&lt;/div&gt;
	&lt;/div&gt;

and the following chain jQuery(‘#containterDiv’).find(‘div’).filter(‘#firstChild’).find(‘span’);

Below is an illustration of how jQuery keeps track of wrapped sets across time:

jquery-wrapped-set-navigation
 

B) Inter-sets navigation

Until now, we only see methods to restrict or filter the current wrapped set. What if we want to come back to the previous set ?

For this, jQuery provides the magical .end() method. This method simply pop the current wrapped set off the stack and goes back to the previous set.

Now that we understand how the sets are stacked, it’s easier to navigate through DOM sections using jQuery methods chaining. Let’s illustrate it by a more complex example:


jQuery (‘#containerDiv’)
.find(‘#firstChild’).attr(‘id’,”)
.find(‘span’).addClass(‘big’)
.end()
.append(‘After Test‘)
.end()
.find(‘#secondChild’).attr(‘id’,”)
.find(‘span’).addClass(‘bigger’);

jquery-chain-example-part1
jquery-chain-example-part2

As you can see, the chaining feature is quite powerfull. You can navigate back and forth in the wrapped-set stack with the .end() function. It is even possible to chain multiple .end() successively to navigate many wrapped sets back. It can becomes quickly unreadable so do not abuse of this function too much.

There is a good practice, when using complex chaining, to indent properly your code to make it at least readable and understandable for future developers. The previous chain can be turned into:


jQuery(‘#containterDiv’)
    .find(‘#firstChild’)
        .attr(‘id’,”)
        .find(‘span’)
            .addClass(‘big’).end()
        .append(‘After Test‘).end()
    .find(‘#secondChild’)
        .attr(‘id’,”)
        .find(‘span’)
            .addClass(‘bigger’);

Each indentation level corresponds to a wrapped set change.

 
 

1 Comment

  1. Pingback: HTML templating with jQuery « DuyHai's Java Blog

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.