Book Image

Instant jQuery Selectors

By : Aurelio De Rosa
Book Image

Instant jQuery Selectors

By: Aurelio De Rosa

Overview of this book

jQuery's selectors is one of the most important concepts of the jQuery library. Usually, the first thing you do is to select one or more elements of a page in order to manipulate them. Therefore, to efficiently learn the usage of jQuery's selectors is one of the first steps to successfully build your website. Instant jQuery Selectors is a practical guide that will teach you how to use jQuery's selectors efficiently in order to easily select theelements of your pages to operate upon with jQuery's methods. You will go through the most common problems that you could face while developing your project and will learn how to solve them with the help of focused examples that Instant jQuery Selectors has to offer. Instant jQuery Selectors, starting from how to set up jQuery and how to choose the right version for your project, explains to you all the selectors available using a multitude of simple and practical recipes that will help you start using selectors in the right way. Reading the presented recipes you'll learn about the numerous selectors and filters of jQuery – almost 100 of them! You will see how to reuse collections, the methods to filter collections, and how to take advantage of the lesser known parameter of jQuery's constructor: context. Also, you'll discover the methods to traverse the DOM and the techniques to improve the performance of your website by just tweaking selectors. Instant jQuery Selectors is the resource to learn everything you need to know about jQuery's selectors.
Table of Contents (7 chapters)

Other filters (Become an expert)


So far, we've seen a plethora of filters, but the good thing is that jQuery has a lot more than that. Here, I'll show the remaining filters that I haven't added to the previous categories.

Getting ready

The filters are as follows:

Name

Description

:animated

Selects all of the elements that are in the progress of an animation at the time the selector is run.

:contains()

Retrieves the elements that contain the text given. The search is performed among the element itself and all of the descendants.

:empty

Collects the elements that have no children (including text nodes).

:focus

Select the element that has the focus at the time the selector is run.

:has()

Selects elements which contain at least one element that matches the specified selector. The search is performed among all of the descendants, not only the children.

:header

Retrieves all of the headers (h1, h2, h3, h4, h5, and h6)

:hidden

Selects all elements that are hidden. An element is considered hidden, not only if it has display: hidden applied, but also if it's physically not shown (for example, if it has width and height set to zero). More on this here: http://api.jquery.com/hidden-selector/.

:lang()

Selects all elements of the specified language.

:not()

Collects the elements that do not match the given selector.

:parent

Selects the elements that have at least one child node (either an element or text).

:root

Retrieves the element which is the root of the document.

:target

Selects the target element indicated by the fragment identifier of the document's URI.

:visible

Selects all elements that are visible. An element is considered visible if it occupies space in the page. So, also if an element has visibility: hidden applied, thus not shown, it's retrieved because it still occupies space. More information is available at http://api.jquery.com/visible-selector/.

How to do it...

To perform the task, follow these steps:

  1. Create a copy of the child-filters.html file and rename it as other-filters.html.

  2. Edit the <head> section of the page adding this code:

    <script>
        $(document).ready(function() {
            $('h2:contains(jQuery)').css('background-color', '#976FAC');
            console.log($('div:has(p.border)').length);
            console.log($('div:not(.wrapper)').length);
        });
    </script>
  3. Save the file, open it with your favorite browser and take a look at the console.

How it works...

In the first line of our anonymous function, we're selecting all of the <h2> instances that contain the text jQuery. To achieve this goal, we're relying on the :contains() filter passing it to the string we're searching for. Looking at the code, it isn't hard to see that the only element that matches is the first <h2> contained in the <div id="content"> section. Once retrieved, we apply a violet background color to it.

In the second task, we're searching for all of the <div> instances that have at least one paragraph which have the class "border" applied. Since both the <div> instances of our page have at least one paragraph that matches (precisely both have just one of those paragraphs), the selection retrieves them all. Once done, we print the length of the collection on the console that is 2.

The last statement collects all of the <div> instances that don't have a class wrapper applied. Once again, looking at the HTML source, you can see that the first <div> section is the only one having the class applied. So, our selection will only retrieve the second <div>, and this explains why the console displays the number 1 in the last line.