An introduction to jQuery selectors and traversal methods, and their use in navigating the DOM

Original: http://www.learningjquery.com/2006/11/how-to-get-anything-you-want-part-1
Author: Karl Swedberg

Similar Tutorials: SelectorsAttributesTraversing

This article provides an introduction into the world of jQuery selectors: how to use them, and what they're good for.

Contents

[edit]

Part I

[edit]

Introduction

With jQuery, you can get to just about anything on a web page. In this entry, I’ll show you a few ways you can use jQuery’s versatile selector expressions to traverse to an element — or group of elements — in the DOM.

I’ve put together a short paragraph and an ordered list scattered with inline elements such as <em>, <code>, and links. It’s all wrapped in a DIV with an ID of "jqdt," which allows me to focus the demonstration to one area of this page.

Each button below the "jqdt" DIV will toggle a yellow background on and off for the parts of the DIV described.

[edit]

Demo

This is a paragraph of text with class=”goofy.” It has an external link, some $(code), and a #dt-link3 same-page link.

  1. list item 1 with dummy link to silly.pdf
  2. list item 2 with class=”goofy
  3. list item 3 SURPRISE!
  4. list item 4 with silly link to silly.pdf silly.pdf

Note: The selectors used for the toggle buttons are identical to the ones shown next to each button, except that they are preceded by $('#jqdt').find to target the highlighting.

jQuery’s selector expressions cover the full range of CSS 1-3, along with some custom, jQuery-only expressions.

In the follow-up to this tutorial, I’ll explore jQuery methods such as .filter(), prev(), and siblings() that complement the above selector expressions to give you full DOM-traversing power!

[edit]

Part II

[edit]

Introduction

Now that we have looked at traversing the DOM with jQuery’s selector expressions to get any elements in the document, I’m going to focus on jQuery methods that provide even more ways to get elements. Some of these methods have a nearly identical counterpart among the selector expressions, but for the most part, the two ways of getting elements complement each other.

Once again, I’ve put together a short paragraph and an ordered list scattered with inline elements such as <em>, <code>, and links. This time it’s wrapped in a DIV with an ID of "jqdt2," which allows me to focus the demonstration to one area of this page.

Each button below the "jqdt2" DIV will toggle a background color on and off for the parts of the DIV described.

[edit]

Demo

This is a paragraph of text with class=”goofy.” It has an external link, some $(code), and a same-page link.

  1. list item 1 with dummy link to silly.pdf
  2. list item 2 with class=”goofy
  3. list item 3 with class=”funny”
  4. list item 4 with silly link to silly.pdf
  5. list item 5 with class=”goofy

Click a link to toggle the selector's highlighting

For a complete list of DOM traversal methods, check out the Traversing section of the API. In addition to the ones covered in this entry, you’ll find: