jQuery website jQuery documentation

Core

$(String expr, Element|jQuery context) : jQuery

exprStringAn expression to search with
contextElement|jQuery(optional) A DOM Element, Document or jQuery to use as context

This function accepts a string containing a CSS or basic XPath selector which is then used to match a set of elements.

The core functionality of jQuery centers around this function. Everything in jQuery is based upon this, or uses this in some way. The most basic use of this function is to pass in an expression (usually consisting of CSS or XPath), which then finds all matching elements.

By default, $() looks for DOM elements within the context of the current HTML document.

Examples

Finds all p elements that are children of a div element.

Before
<p>one</p> <div><p>two</p></div> <p>three</p>
Code
$("div > p")
Result
[ <p>two</p> ]

Searches for all inputs of type radio within the first form in the document

Code
$("input:radio", document.forms[0])

This finds all div elements within the specified XML document.

Code
$("div", xml.responseXML)

$(String html) : jQuery

htmlStringA string of HTML to create on the fly.

Create DOM elements on-the-fly from the provided String of raw HTML.

Example

Creates a div element (and all of its contents) dynamically, and appends it to the element with the ID of body. Internally, an element is created and it's innerHTML property set to the given markup. It is therefore both quite flexible and limited.

Code
$("<div><p>Hello</p></div>").appendTo("#body")

$(Element|Array elems) : jQuery

elemsElement|ArrayDOM element(s) to be encapsulated by a jQuery object.

Wrap jQuery functionality around a single or multiple DOM Element(s).

This function also accepts XML Documents and Window objects as valid arguments (even though they are not DOM Elements).

Examples

Sets the background color of the page to black.

Code
$(document.body).background( "black" );

Hides all the input elements within a form

Code
$( myForm.elements ).hide()

$(Function fn) : jQuery

fnFunctionThe function to execute when the DOM is ready.

A shorthand for $(document).ready(), allowing you to bind a function to be executed when the DOM document has finished loading. This function behaves just like $(document).ready(), in that it should be used to wrap all of the other $() operations on your page. While this function is, technically, chainable - there really isn't much use for chaining against it. You can have as many $(document).ready events on your page as you like.

See ready(Function) for details about the ready event.

Examples

Executes the function when the DOM is ready to be used.

Code
$(function(){
  // Document is ready
});

Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.

Code
jQuery(function($) {
  // Your code using failsafe $ alias here...
});

jquery() : String

The current version of jQuery.

length() : Number

The number of elements currently matched.

Example

Before
<img src="test1.jpg"/> <img src="test2.jpg"/>
Code
$("img").length;
Result
2

size() : Number

The number of elements currently matched.

Example

Before
<img src="test1.jpg"/> <img src="test2.jpg"/>
Code
$("img").size();
Result
2

get() : Array

Access all matched elements. This serves as a backwards-compatible way of accessing all matched elements (other than the jQuery object itself, which is, in fact, an array of elements).

Example

Selects all images in the document and returns the DOM Elements as an Array

Before
<img src="test1.jpg"/> <img src="test2.jpg"/>
Code
$("img").get();
Result
[ <img src="test1.jpg"/> <img src="test2.jpg"/> ]

get(Number num) : Element

numNumberAccess the element in the Nth position.

Access a single matched element. num is used to access the Nth element matched.

Example

Selects all images in the document and returns the first one

Before
<img src="test1.jpg"/> <img src="test2.jpg"/>
Code
$("img").get(0);
Result
[ <img src="test1.jpg"/> ]

pushStack(Elements elems) : jQuery

elemsElementsAn array of elements

Set the jQuery object to an array of elements, while maintaining the stack.

Example

Code
$("img").pushStack([ document.body ]);
Result
$("img").pushStack() == [ document.body ]

setArray(Elements elems) : jQuery

elemsElementsAn array of elements

Set the jQuery object to an array of elements. This operation is completely destructive - be sure to use .pushStack() if you wish to maintain the jQuery stack.

Example

Code
$("img").setArray([ document.body ]);
Result
$("img").setArray() == [ document.body ]

each(Function fn) : jQuery

fnFunctionA function to execute

Execute a function within the context of every matched element. This means that every time the passed-in function is executed (which is once for every element matched) the 'this' keyword points to the specific element.

Additionally, the function, when executed, is passed a single argument representing the position of the element in the matched set.

Example

Iterates over two images and sets their src property

Before
<img/><img/>
Code
$("img").each(function(i){
  this.src = "test" + i + ".jpg";
});
Result
<img src="test0.jpg"/><img src="test1.jpg"/>

index(Element subject) : Number

subjectElementObject to search for

Searches every matched element for the object and returns the index of the element, if found, starting with zero. Returns -1 if the object wasn't found.

Examples

Returns the index for the element with ID foobar

Before
<div id="foobar"><b></b><span id="foo"></span></div>
Code
$("*").index( $('#foobar')[0] )
Result
0

Returns the index for the element with ID foo within another element

Before
<div id="foobar"><b></b><span id="foo"></span></div>
Code
$("*").index( $('#foo')[0] )
Result
2

Returns -1, as there is no element with ID bar

Before
<div id="foobar"><b></b><span id="foo"></span></div>
Code
$("*").index( $('#bar')[0] )
Result
-1

domManip(Array args, Boolean table, Number dir, Function fn) : jQuery

argsArray
tableBooleanInsert TBODY in TABLEs if one is not found.
dirNumberIf dir<0, process args in reverse order.
fnFunctionThe function doing the DOM manipulation.

$.extend(Object prop) : Object

propObjectThe object that will be merged into the jQuery object

Extends the jQuery object itself. Can be used to add functions into the jQuery namespace and to add plugin methods (plugins).

Examples

Adds two plugin methods.

Code
jQuery.fn.extend({
  check: function() {
    return this.each(function() { this.checked = true; });
  },
  uncheck: function() {
    return this.each(function() { this.checked = false; });
  }
});
$("input[@type=checkbox]").check();
$("input[@type=radio]").uncheck();

Adds two functions into the jQuery namespace

Code
jQuery.extend({
  min: function(a, b) { return a < b ? a : b; },
  max: function(a, b) { return a > b ? a : b; }
});

$.noConflict() : undefined

Run this function to give control of the $ variable back to whichever library first implemented it. This helps to make sure that jQuery doesn't conflict with the $ object of other libraries.

By using this function, you will only be able to access jQuery using the 'jQuery' variable. For example, where you used to do $("div p"), you now must do jQuery("div p").

Examples

Maps the original object that was referenced by $ back to $

Code
jQuery.noConflict();
// Do something with jQuery
jQuery("div p").hide();
// Do something with another library's $()
$("content").style.display = 'none';

Reverts the $ alias and then creates and executes a function to provide the $ as a jQuery alias inside the functions scope. Inside the function the original $ object is not available. This works well for most plugins that don't rely on any other library.

Code
jQuery.noConflict();
(function($) { 
  $(function() {
    // more code using $ as alias to jQuery
  });
})(jQuery);
// other code using $ as an alias to the other library

eq(Number pos) : jQuery

posNumberThe index of the element that you wish to limit to.

Reduce the set of matched elements to a single element. The position of the element in the set of matched elements starts at 0 and goes to length - 1.

Example

Before
<p>This is just a test.</p><p>So is this</p>
Code
$("p").eq(1)
Result
[ <p>So is this</p> ]

lt(Number pos) : jQuery

posNumberReduce the set to all elements below this position.

Reduce the set of matched elements to all elements before a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.

Example

Before
<p>This is just a test.</p><p>So is this</p>
Code
$("p").lt(1)
Result
[ <p>This is just a test.</p> ]

gt(Number pos) : jQuery

posNumberReduce the set to all elements after this position.

Reduce the set of matched elements to all elements after a given position. The position of the element in the set of matched elements starts at 0 and goes to length - 1.

Example

Before
<p>This is just a test.</p><p>So is this</p>
Code
$("p").gt(0)
Result
[ <p>So is this</p> ]

$.find() : Array