Pattern of using jQuery
The typical pattern of using jQuery is
- selecting a DOM element(s)
- 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.
Method | Description |
---|---|
.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
JavaScript Book
jQuery
Introduction:
- The jQuery Basics
- Pattern of using jQuery
- Chain methods jQuery
- Using jQuery with Other Javascript Libraries