Selecting Elements with jQuery

By Lynn Beighley

Part of jQuery For Dummies Cheat Sheet

jQuery allows you to select elements in a Web page with ease. You can find anything you want in a page and then use jQuery to add special effects, react to user actions, or show and hide content inside or outside the element you have selected. All these tasks start with knowing how to select an element. Here’s a handy list you can use to select almost anything on your Web page.

To Select By Use This
Element type (for example, <img>) $(“img”)
All elements $(“*”)
ID (for example, id=”thisIsTheID”) $(“#thisIsTheID”)
Class (for example, class=”someClass”) $(“.someClass”)
Order (for example, the first or last <img> element) $(“img:first”)
Attribute, (for example, to get the length attribute of <img>) $(“img[height]”).length
Parent (for example, the parent of <div>) $(“div:parent”)
Child (for example, the first or last child of <div>) $(“div:first-child”)