Pattern of using jQuery

The typical pattern of using jQuery is

  1. selecting a DOM element(s)
  2. performing some operation or manipulation on the selected element(s).

The basic call to jQuery is

$(selector);

where selector is an expression for matching HTML elements.

The selector format is the same used with CSS.

For example, # matches elements by their id attribute and . matches by CSS classes.

To get a div with the ID myDiv use the following:

$("div#myDiv");

$() returns a wrapper object.

A wrapper object is array-like.

When you call a jQuery method, it applies the method to all of the selected elements.

There's no need to iterate over the collection with a loop.

The following table shows several methods from the jQuery object.

MethodDescription
.ready()Specifies a function to run when the DOM is fully loaded.
.click()Sets click event handlers on the set of matched elements.
.addClass()Adds a CSS class to the set of matched elements.
.removeClass()Removes a CSS class from the set of matched elements.
Home 
  JavaScript Book 
    jQuery  

Introduction:
  1. The jQuery Basics
  2. Pattern of using jQuery
  3. Chain methods jQuery
  4. Using jQuery with Other Javascript Libraries