Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. Web Development

jQuery Succinctly: jQuery Selecting

This post is part of a series called jQuery Succinctly.
jQuery Succinctly: Core jQuery
jQuery Succinctly: Traversing With jQuery

Custom jQuery Filters Can Select Elements When Used Alone

It is not necessary to provide an actual element in conjunction with a filter, such as $('div:hidden'). It is possible to simply pass the filter alone, anywhere a selector expression is expected.

Some examples:

Grokking the :Hidden and :Visible Filter

The custom jQuery selector filters :hidden and :visible do not take into account the CSS visibility property as one might expect. The way jQuery determines if an element is hidden or visible is if the element consumes any space in the document. To be exact, an element is visible if its browser-reported offsetWidth or offsetHeight is greater than 0. That way, an element that might have a CSS display value of block contained in an element with a display value of none would accurately report that it is not visible.

Examine the code carefully and make sure you understand why the value returned is true even though the <div> being selected has an inline style of display:block.

Using the Is() Method to Return a Boolean Value

It is often necessary to determine if the selected set of elements does, in fact, contain a specific element. Using the is() method, we can check the current set against an expression/filter. The check will return true if the set contains at least one element that is selected by the given expression/filter. If it does not contain the element, a false value is returned. Examine the following code:

It should be apparent that the second alert() will return a value of false because our wrapper set did not contain a <div> that had an id attribute value of i2. The is() method is quite handy for determining if the wrapper set contains a specific element.

Notes: As of jQuery 1.3, the is() method supports all expressions. Previously, complex expressions such as those containing hierarchy selectors (such as +, ~, and >) always returned true.

Filter is used by other internal jQuery functions. Therefore, all rules that apply there, apply here, as well.

Some developers use is('.class') to determine if an element has a specific class. Don't forget that jQuery already has a method for doing this called hasClass('class'), which can be used on elements that contain more than one class value. But truth be told, hasClass() is just a convenient wrapper for the is() method.

You Can Pass jQuery More Than One Selector Expression

You can provide the jQuery function's first parameter several expressions separated by a comma: $('expression, expression, expression'). In other words, you are not limited to selecting elements using only a single expression. For example, in the example below, I am passing the jQuery function three expressions separated by a comma.

Each of these expressions selects DOM elements that are all added to the wrapper set. We can then operate on these elements using jQuery methods. Keep in mind that all the selected elements will be placed in the same wrapper set. An inefficient way to do this would be to call the jQuery function three times, once for each expression.

Checking Wrapper Set .length to Determine Selection

It is possible to determine if your expression has selected anything by checking if the wrapper set has a length. You can do so by using the array property length. If the length property does not return 0, then you know at least one element matches the expression you passed to the jQuery function. For example, in the code below we check the page for an element with an id of "notHere." Guess what? It is not there!

Notes: If it is not obvious, the length property can also report the number of elements in the wrapper set - stated another way, how many elements were selected by the expression passed to the jQuery function.

Creating Custom Filters for Selecting Elements

The capabilities of the jQuery selector engine can be extended by creating your own custom filters. In theory, all you are doing here is building upon the custom selectors that are already part of jQuery. For example, say we would like to select all elements on a Web page that are absolutely positioned. Since jQuery does not already have a custom :positionAbsolute filter, we can create our own.

The most important thing to grasp here is that you are not limited to the default selectors provided by jQuery. You can create your own. However, before you spend the time creating your own version of a selector, you might just simply try the filter() method with a specified filtering function. For example, I could have avoided writing the :positionAbsolute selector by simply filtering the <div> elements in my prior example with a function I pass to the filter() method.

Notes: For additional information about creating your own selectors I suggest the following read: http://www.bennadel.com/blog/1457-How-To-Build-A-Custom-jQuery-Selector.htm

Differences Between Filtering By Numeric Order vs. DOM Relationships

jQuery provides filters for filtering a wrapper set by an element's numerical context within the set.

These filters are:

  • :first
  • :last
  • :even
  • :odd
  • :eq(index)
  • :gt(index)
  • :lt(index)

Notes: Filters that filter the wrapper set itself do so by filtering elements in the set at a starting point of 0, or index of 0. For example :eq(0) and :first access the first element in the set - $('div:eq(0)') - which is at a 0 index. This is in contrast to the :nth-child filter that is one-indexed. Meaning, for example, :nth-child(1) will return the first child element, but trying to use :nth-child(0) will not work. Using :nth-child(0) will always select nothing.

Using :first will select the first element in the set while :last will select the last element in the set. Remember that they filter the set based on the relationship (numerical hierarchy starting at 0) within the set, but not the elements' relationships in the context of the DOM. Given this knowledge, it should be obvious why the filters :first, :last, and :eq(index) will always return a single element.

If it is not obvious, allow me to explain further. The reason that :first can only return a single element is because there can only be one element in a set that is considered first when there is only one set. This should be fairly logical. Examine the code below to see this concept in action.

With a clear understanding of manipulating the set itself, we can augment our understanding of selecting elements by using filters that select elements that have unique relationships with other elements within the actual DOM. jQuery provides several selectors to do this. Some of these selectors are custom, while some are well known CSS expressions for selecting DOM elements.

  • ancestor descendant
  • parent > child
  • prev + next
  • prev ~ siblings
  • :nth-child(selector)
  • :first-child
  • :last-child
  • :only-child
  • :empty
  • :has(selector)
  • :parent

Usage of these selector filters will select elements based on their relationship within the DOM as pertaining to other elements in the DOM. To demonstrate this concept, let's look at some code.

If you are surprised by the fact that $('li:nth-child(odd)').text() returns the value 135135, you are not yet grokking relationship filters. The statement, $('li:nth-child(odd)') said verbally would be "find all <li> elements in the Web page that are children, and then filter them by odd children." Well, it just so happens that there are two structures in the page that have a grouping of siblings made up of <li>s. My point is this: The wrapper set is made up of elements based on a filter that takes into account an element's relationship to other elements in the DOM. These relationships can be found in multiple locations.

The concept to take away is that not all filters are created equally. Make sure you understand which ones filter based on DOM relationships-e.g. :only-child-and which ones filter by the elements' position-e.g. :eq()-in the wrapped set.

Selecting Elements By Id When the Value Contains Meta-Characters

jQuery selectors use a set of meta-characters (e.g. # ~ [] = > ) that when used as a literal part of a name (e.g. id="#foo[bar]") should be escaped. It is possible to escape characters by placing two backslashes before the character. Examine the code below to see how using two backslashes in the selection expression allows us to select an element with an id attribute value of #foo[bar].

Here is the complete list of characters that need to be escaped when used as a literal part of a name.

  • #
  • ;
  • &
  • ,
  • .
  • +
  • *
  • ~
  • '
  • :
  • "
  • !
  • ^
  • $
  • [
  • ]
  • (
  • )
  • =
  • >
  • |
  • /

Stacking Selector Filters

It is possible to stack selector filters-e.g. a[title="jQuery"][href^="http://"]. The obvious example of this is selecting an element that has specific attributes with specific attribute values. For example, the jQuery code below will only select <a> elements in the HTML page that:

  • Contain an href attribute with a starting value of "http://"
  • Have a title attribute with a value of "jQuery"

Only one <a> is being selected.

Notice in the code how we have stacked two filters to accomplish this selection.

Other selector filters can be stacked besides just attribute filters. For example:

The concept to take away is that selector filters can be stacked and used in combination.

Notes: You can also nest and stack filters - e.g. $('p').filter(':not(:first):not(:last)')

Nesting Selector Filters

Selector filters can be nested. This enables you to wield filters in a very concise and powerful manner. Below, I give an example of how you can nest filters to perform complex filtering.

The concept to take away is that selector filters can be nested.

Notes: You can also nest and stack filters - e.g. $('p').filter(':not(:first):not(:last)')

Grokking the :nth-child() Filter

The :nth-child() filter has many uses. For example, say you only want to select every third <li> element contained within a <ul> element. It is possible with the :nth-child() filter. Examine the following code to get a better understanding of how to use the :nth-child() filter.

Selecting Elements By Searching Attribute Values Using Regular Expressions

When the jQuery attribute filters used to select elements are not robust enough, try using regular expressions. James Padolsey has written a nice extension to the filter selectors that will allow us to create custom regular expressions for filtering. I have provided a code example here, but make sure you also check out the article on http://james.padolsey.com for all the details.

Difference Between Selecting Direct Children vs. All Descendants

Direct children elements only can be selected by using the combiner > or by way of the children() traversing method. All descendants can be selected by using the * CSS expression. Make sure you clearly understand the difference between the two. The example below demonstrates the differences.

Selecting Direct Child Elements When a Context Is Already Set

It is possible to use the combiner > without a context to select direct child elements when a context has already been provided. Examine the code below.

Basically, '> element' can be used as an expression when a context has already been determined.

Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.