Arguments
jQuery( selector, [ context ] )
jQuery( element )
jQuery( elementArray )
jQuery( jQuery object )
jQuery( )
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:
$('div.foo');
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, if within a callback function we wish to do a search for an element, we can restrict that search:
Since we've restricted the span selector 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 allow us to create a jQuery object using a DOM element or elements that we have already found in some other way. 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:
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 wrapped in a jQuery object before we can call jQuery methods on it.
When XML data is returned from an Ajax call, we can use the $() function to wrap it in a jQuery object that we can easily work with. Once this is done, we can retrieve individual elements of the XML structure using .find() and other DOM traversal methods.
Cloning jQuery Objects
When a jQuery object is passed as a parameter 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, if you pass no arguments in to the jQuery() method, an empty jQuery set will be returned. In previous versions of jQuery, a set containing the document node would be returned.
Examples
Example 1
Finds all p elements that are children of a div element.<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $("div > p").css("border", "1px solid gray"); }); </script> </head> <body> <p>one</p> <div><p>two</p></div> <p>three</p> </body> </html>
Example 2
Finds all inputs of type radio within the first form in the document.
$("input:radio", document.forms[0]);
Example 3
Finds all div elements within an XML document from an Ajax response.
$("div", xml.responseXML);
Example 4
Sets the background color of the page to black.$(document.body).css( "background", "black" );
Example 5
Hides all the input elements within a form.$(myForm.elements).hide()
Arguments
jQuery( html, [ ownerDocument ] )
jQuery( html, props )
Creating New Elements
If a string is passed as the parameter to $(), jQuery examines the string to see if it looks like HTML (i.e., it has somewhere within the string). If not, the string is interpreted as a selector expression, as explained above. But if the string appears to be an HTML snippet, jQuery attempts to create new DOM elements as described by the HTML. Then a jQuery object is created and returned that refers to these elements. We can perform any of the usual jQuery methods on this object:
$('<p id="test">My <em>new</em> text</p>').appendTo('body');
innerHTML mechanism. Specifically, jQuery creates a new <div> element and sets the innerHTML property of the element to the HTML snippet that was passed in. When the parameter has a single tag, such as $('<img />') or $('<a></a>'), jQuery creates the element using the native JavaScript createElement() function.
$('<a href="http://xhtml.co.il/en/"></a>');
Alternatively, jQuery allows XML-like tag syntax (with or without a space before the slash):
$('<a/>');
Tags that cannot contain elements may be quick-closed or not:
$('<img />');
$('<input>');
Examples
Example 1
Creates a div element (and all of its contents) dynamically, and appends it to the body element. Internally, an element is created and its innerHTML property set to the given markup. It is therefore both quite flexible and limited.$("<div><p>Hello</p></div>").appendTo("body")
Example 2
Create some DOM elements.$("<div/>", { "class": "test", text: "Click me!", click: function(){ $(this).toggleClass("test"); } }).appendTo("body"); $("<input>", { type: "text", val: "Test", focusin: function() { $(this).addClass("active"); }, focusout: function() { $(this).removeClass("active"); } }).appendTo("form");
Arguments
jQuery( callback )
$(document).ready(), in that it should be used to wrap other $() operations on your page that depend on the DOM being ready. While this function is, technically, chainable, there really isn't much use for chaining against it.Examples
Example 1
Executes the function when the DOM is ready to be used.
$(function(){
// Document is ready
});
Example 2
Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.
jQuery(function($) {
// Your code using failsafe $ alias here...
});

