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)

Preface

jQuery is a popular JavaScript library designed to simplify the client-side scripting of HTML, widely used by most of the websites on the Internet. It helps you to manipulate elements, and create fancy interactive websites in a very easy way that is compatible with modern and older browsers as well.

Instant jQuery Selectors will take you as a beginner and turn you into an expert that knows all of the tips and tricks of the pros about the jQuery's basic element: selectors. Through the use of examples, you'll be able to start using selectors efficiently and easily to solve your day-to-day problems.

What this book covers

Setting up jQuery (Must know) explains how to download and include jQuery in a web page and a good rule of thumb to choose between the two major jQuery branches, 1.X and 2.X.

The All selector (Must know) covers the All (or Universal) selector, to select all of the elements of a document.

Selecting by ID (Must know) introduces the ID selector and how jQuery manages it behind the scenes.

Selecting by class (Must know) describes the Class selector and how to optimize it for older browsers.

Selecting by tag (Must know) demonstrates the use of the Element selector and how you can use it in conjunction with other selectors.

Multiple selectors at once (Should know) shows how to use more than one selector in a single call.

Selecting by hierarchy (Must know) covers the hierarchy operators that allow you to retrieve elements by their relationship in the DOM.

Selecting by attributes (Should know) describes how many and what are the selectors to collect elements by their attributes.

Selecting by position using filters (Should know) introduces filters, and in particular those that allow to filter a collection using the elements' positions inside the collection itself.

Selecting from elements using filters (Should know) covers filters specific to form elements.

Child filters (Should know) describes filters to target node's children.

Other filters (Become an expert) shows the filters not covered in the previous recipes.

Custom filters (Become an expert) explains how to create a custom filter using two approaches.

Context matters (Should know) shows how to use the second parameter of the jQuery constructor in order to improve performance.

Improving performance re-using selected elements (Become an expert) demonstrates how to store a collection of previously selected elements in a variable for a later processing and how this method can improve performances.

Methods to filter collections (Become an expert) covers how many and what are the jQuery's methods to filter a collection.

Traversing DOM SubTrees (Become an expert) describes how many and what are the methods that can be applied to a collection, to find elements starting from a matched set.

How to have efficient selectors (Become an expert) explains tips and tricks to improve the performances of a website by simply selecting elements in the right way.

What you need for this book

This book assumes the reader to have a basic understanding of HTML and CSS. In addition, having basic knowledge of JavaScript, its syntax, and some concepts like anonymous functions, events, DOM and callbacks would be beneficial.

Who this book is for

The book is for web developers who want to delve into jQuery, the most popular and adopted JavaScript framework, from its very starting point: selectors. Even if you're already familiar with the framework and its selectors, you could find several tips and tricks that you aren't aware of, especially about performance and how jQuery acts behind the scenes.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text are shown as follows: "We can include other contexts through the use of the include directive."

A block of code is set as follows:

<!DOCTYPE html>
<html>
   <head>
      <meta charset="UTF-8">
      <title>Instant jQuery Selector How-to</title>
   </head>
   <body>
   </body>
</html>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<script src="jquery-1.10.1.min.js"></script>
    <script>
        $(document).ready(function() {
            $('*').css('border', '2px solid #000000');
        });
    </script>

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "clicking the Next button moves you to the next screen".

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.