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)

Selecting by ID (Must know)


In this section we'll learn how to select an element by its ID and apply the same border seen previously.

How to do it...

This task can be achieved by performing the following instructions:

  1. Create a copy of the template.html file and rename it as selecting-by-id.html.

  2. Inside the <body> tag, add the following HTML markup:

    <h1 id="title">The Id selector</h1>
    <div id="container">
        This example shows you how to use the Id selector.
        <p id="description">As you can see, this time the border is applied only to the h1 element because of its id.</p>
        <span id="note">Hey, I'm a note</span>
    </div>
  3. Edit the <head> section as we did in the previous chapter, but this time add this script after the jQuery library instead:

    <script>
        $(document).ready(function() {
            $('#title').css('border', '2px solid #000000');
        });
    </script>
  4. Save the file and open it with your favorite browser.

How it works...

The code shown is very similar to the previous code, but for the purpose of the task, we've applied an id to all of the elements children of <body>. Also, the texts have changed, but this is much less important.

The core part of our code is the third step where, instead of the All selector, we've used the ID selector. In our example, we've used it to pick up the <h1> having as id title and then apply the border. The ID selector is characterized by the sign # prepended to the value of the element's ID attribute and using it, jQuery returns a collection of either zero or one DOM element. It's important to highlight that this selector is surely the fastest one, regardless of the browser used since when it turns to deal with an ID, behind the scenes jQuery uses the JavaScript function document.getElementById(), which is very efficient.

Remember that, although doable, having more than one element with the same ID is invalid and must be avoided. However, in case you decided to ignore this rule (Please don't! Really!), be aware that jQuery will retrieve only the first matched element encountered. Moreover, just like the other selectors that we'll see, when no match is found, an empty collection is returned.

There's more...

There are many characters you can use to set the ID of your elements, but you need to be aware that some of them need to be treated carefully.

Escaping special characters

The value of an ID doesn't allow just alphabetical characters, but also: dots, hyphens, semicolons, and others as regulated by the W3C (http://www.w3.org/TR/html4/types.html#type-id). You are free to use each of the described characters, but to tell jQuery to treat these characters literally rather than as CSS notation they must be escaped. You can escape them by prepending them with two backslashes.

So, if we want to select an element having ID .title, we would have to write $('#\\.title').

Storing a collection

Once you performed a selection, the matched collection of elements can be stored in a variable for a later processing. The usual convention to name these variables is to write a dollar sign in front of the variable's name to highlight that it contains a jQuery object. Here is a example:

var $title = $('#title');
var $borderedTitle = $('#title').css('border', '2px solid #000000');