jQuery( selector [, context ] )Returns: jQuery
Description: Accepts a string containing a CSS selector which is then used to match a set of elements.
version added: 1.0jQuery( selector [, context ] )
- selectorType: selectorA string containing a selector expression
- contextA DOM Element, Document, or jQuery to use as context
version added: 1.0jQuery( element )
- elementType: ElementA DOM element to wrap in a jQuery object.
version added: 1.0jQuery( elementArray )
- elementArrayType: ArrayAn array containing a set of DOM elements to wrap in a jQuery object.
version added: 1.0jQuery( object )
- objectType: PlainObjectA plain object to wrap in a jQuery object.
version added: 1.0jQuery( jQuery object )
- jQuery objectType: PlainObjectAn existing jQuery object to clone.
version added: 1.4jQuery()
- This method does not accept any arguments.
In the first formulation listed above, jQuery()
— which can also be written as $()
— searches through the DOM for any elements that match the provided selector and creates a new jQuery object that references these elements:
1 |
|
If no elements match the provided selector, the new jQuery object is "empty"; that is, it contains no elements and has .length
property of 0.
Selector Context
By default, selectors perform their searches within the DOM starting at the document root. However, an alternate context can be given for the search by using the optional second parameter to the $()
function. For example, to do a search within an event handler, the search can be restricted like so:
1 2 3 |
|
When the search for the span selector is restricted to the context of this
, only spans within the clicked element will get the additional class.
Internally, selector context is implemented with the .find()
method, so $( "span", this )
is equivalent to $( this ).find( "span" )
.
Using DOM elements
The second and third formulations of this function create a jQuery object using one or more DOM elements that were already selected in some other way.
Note: These formulations are meant to consume only DOM elements; feeding mixed data to the elementArray form is particularly discouraged.
A common use of this facility is to call jQuery methods on an element that has been passed to a callback function through the keyword this
:
1 2 3 |
|
This example causes elements to be hidden with a sliding animation when clicked. Because the handler receives the clicked item in the this
keyword as a bare DOM element, the element must be passed to the $()
function before applying jQuery methods to it.
XML data returned from an Ajax call can be passed to the $()
function so individual elements of the XML structure can be retrieved using .find()
and other DOM traversal methods.
1 2 3 |
|
Cloning jQuery Objects
When a jQuery object is passed to the $()
function, a clone of the object is created. This new jQuery object references the same DOM elements as the initial one.
Returning an Empty Set
As of jQuery 1.4, calling the jQuery()
method with no arguments returns an empty jQuery set (with a .length
property of 0). In previous versions of jQuery, this would return a set containing the document node.
Working With Plain Objects
At present, the only operations supported on plain JavaScript objects wrapped in jQuery are: .data()
,.prop()
,.on()
, .off()
, .trigger()
and .triggerHandler()
. The use of .data()
(or any method requiring .data()
) on a plain object will result in a new property on the object called jQuery{randomNumber} (eg. jQuery123456789).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Should .trigger( "eventName" )
be used, it will search for an "eventName" property on the object and attempt to execute it after any attached jQuery handlers are executed. It does not check whether the property is a function or not. To avoid this behavior, .triggerHandler( "eventName" )
should be used instead.
1 |
|
Examples:
Example: Find all p elements that are children of a div element and apply a border to them.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Example: Find all inputs of type radio within the first form in the document.
1 |
|
Example: Find all div elements within an XML document from an Ajax response.
1 |
|
Example: Set the background color of the page to black.
1 |
|
Example: Hide all the input elements within a form.
1 |
|