Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Instant jQuery Selectors

You're reading from   Instant jQuery Selectors Learn how to master the art of effectively using jQuery's selectors

Arrow left icon
Product type Paperback
Published in Aug 2013
Publisher Packt
ISBN-13 9781783282210
Length 70 pages
Edition 1st Edition
Tools
Arrow right icon
Author (1):
Arrow left icon
Aurelio De Rosa Aurelio De Rosa
Author Profile Icon Aurelio De Rosa
Aurelio De Rosa
Arrow right icon
View More author details
Toc

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');
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image