Selectors
Basics
#id
Selects a single element with the given id attribute.
element
Selects all elements with the given tag name.
Hierarchy
parent > child
Selects all direct child elements specified by "child" of elements specified by "parent".
prev + next
Selects all next elements matching "next" that are immediately preceded by a sibling "prev".
prev ~ siblings
Selects all sibling elements that follow after the "prev" element, have the same parent, and match the filtering "siblings" selector.
Basic Filters
:first
Selects the first matched element.
:last
Selects the last matched element.
:not(selector)
Selects all elements that do not match the given selector.
:even
Selects even elements, zero-indexed.
:odd
Selects odd elements, zero-indexed.
:eq(index)
Select the element at index n within the matched set.
:gt(index)
Select all elements at an index greater than index within the matched set.
:lt(index)
Select all elements at an index less than index within the matched set.
:header
Selects all elements that are headers, like h1, h2, h3 and so on.
:animated
Select all elements that are in the progress of an animation at the time the selector is run.
:focus1.6+
Selects element if it is currently focused.
Content Filters
:empty
Select all elements that have no children (including text nodes).
:has(selector)
Selects elements which contain at least one element that matches the specified selector.
:parent
Select all elements that are the parent of another element, including text nodes.
Visibility Filters
:hidden
Selects all elements that are hidden.
:visible
Selects all elements that are visible.
Child Filters
:first-child
Selects all elements that are the first child of their parent.
:last-child
Selects all elements that are the last child of their parent.
:only-child
Selects all elements that are the only child of their parent.
Attribute Filters
[attribute]
Selects elements that have the specified attribute, with any value.
[attribute=value]
Selects elements that have the specified attribute with a value exactly equal to a certain value.
[attribute!=value]
Select elements that either don't have the specified attribute, or do have the specified attribute but not with a certain value.
[attribute^=value]
Selects elements that have the specified attribute with a value beginning exactly with a given string.
[attribute$=value]
Selects elements that have the specified attribute with a value ending exactly with a given string.
[attribute*=value]
Selects elements that have the specified attribute with a value containing the a given substring.
[attribute|=value]
Selects elements that have the specified attribute with a value either equal to a given string or starting with that string followed by a hyphen (-).
[attribute~=value]
Selects elements that have the specified attribute with a value containing a given word, delimited by spaces.
Forms
:input
Selects all input, textarea, select and button elements.
:text
Selects all elements of type text.
:password
Selects all elements of type password
:radio
Selects all elements of type radio.
:checkbox
Selects all elements of type checkbox.
:submit
Selects all elements of type submit.
:image
Selects all elements of type image.
:reset
Selects all elements of type reset.
:button
Selects all button elements and elements of type button.
:file
Selects all elements of type file.
Form Filters
:enabled
Selects all elements that are enabled
:disabled
Selects all elements that are disabled.
:checked
Matches all elements that are checked.
:selected
Selects all elements that are selected.
Core
jQuery function
$. jQuery( selector [, context] ), . jQuery( element ), . jQuery( elementArray ), . jQuery( jQueryObject ), . jQuery( )
Accepts a string containing a CSS selector which is then used to match a set of elements.
$. jQuery( html [, ownerDocument] ), . jQuery( html, props )
Creates DOM elements on the fly from the provided string of raw HTML.
$. jQuery( fn )
Binds a function to be executed when the DOM has finished loading.
def. when(deferreds)
Provides a way to execute callback functions based on one or more objects, usually Deferred objects that represent asynchronous events.
fnjQuery.sub( )
Creates a new copy of jQuery whose properties and methods can be modified without affecting the original jQuery object.
$. holdReady( hold )
Indicates whether the ready hold is being requested or released
jQuery Object Accessors
$.each( fn(index, element) )
Iterate over a jQuery object, executing a function for each matched element.
num.size( )
Return the number of DOM elements matched by the jQuery object.
, .length
The number of elements in the jQuery object.
str. selector
A selector representing selector originally passed to jQuery().
el. context
The DOM node context originally passed to jQuery(); if none was passed then context will likely be the document.
$. eq( index )
Reduce the set of matched elements to the one at the specified index.
jQuery.error( str )
Takes a string and throws an exception containing it.
[el],el. get( [index] )
Retrieve the DOM elements matched by the jQuery object.
num. index( )index( selector )index( element )
Search for a given element from among the matched elements.
$jQuery.pushStack( elements, [name, arguments] )
Add a collection of DOM elements onto the jQuery stack.
arr. toArray( )
Retrieve all the DOM elements contained in the jQuery set, as an array.
|
Attributes
Attributes
str. attr( name ) | (name , value ) )1.6*
Get the value of an attribute for the first element in the set of matched elements.
$. attr( name, val ), . attr( map ), . attr( name, fn(index, attr) )
Set one or more attributes for the set of matched elements.
$. removeAttr( name )
Remove an attribute from each element in the set of matched elements.
$. prop( name )1.6+
Get the value of a property for the first element in the set of matched elements.
$. removeProp( name )1.6+
Remove a property for the set of matched elements.
Class
$. addClass( class ), . addClass( fn(index, class) )
Adds the specified class(es) to each of the set of matched elements.
bool. hasClass( class )
Determine whether any of the matched elements are assigned the given class.
$. removeClass( [class] ), . removeClass( fn(index, class) )
Remove one or all classes from each element in the set of matched elements.
$. toggleClass( class [, switch] ), . toggleClass( fn(index, class) [, switch] )
Add or remove a class from each element in the set of matched elements, depending on either the class's presence or the value of the switch argument.
HTML, text
str. html( )
Get the HTML contents of the first element in the set of matched elements.
$. html( val ), . html( fn(index, html) )
Set the HTML contents of each element in the set of matched elements.
str. text( )
Get the combined text contents of each element in the set of matched elements, including their descendants.
$. text( val ), . text( fn(index, html) )
Set the content of each element in the set of matched elements to the specified text.
Value
str,arr. val( )
Get the current value of the first element in the set of matched elements.
$. val( val ), . val( fn() )
Set the value of each element in the set of matched elements.
CSS
CSS
str. css( name )
Get the value of a style property for the first element in the set of matched elements.
$. css( name, val ), . css( map ), . css( name, fn(index, val) )
Set one or more CSS properties for the set of matched elements.
Positioning
obj. offset( )
Get the current coordinates of the first element in the set of matched elements, relative to the document.
$. offset( coord ), . offset( fn( index, coord ) )
Set the current coordinates of every element in the set of matched elements, relative to the document.
$. offsetParent( )
Get the closest ancestor element that is positioned.
obj. position( )
Get the current coordinates of the first element in the set of matched elements, relative to the offset parent.
int. scrollTop( )
Get the current vertical position of the scroll bar for the first element in the set of matched elements.
$. scrollTop( val )
Set the current vertical position of the scroll bar for each of the set of matched elements.
int. scrollLeft( )
Get the current horizontal position of the scroll bar for the first element in the set of matched elements.
$. scrollLeft( val )
Set the current horizontal position of the scroll bar for each of the set of matched elements.
Height and Width
int. height( )
Get the current computed height for the first element in the set of matched elements.
$. height( val ), . height( fn(index, height ) )
Set the CSS height of every matched element.
int. width( )
Get the current computed width for the first element in the set of matched elements.
$. width( val ), .width(( fn(index, height ) )
Set the CSS width of each element in the set of matched elements.
int. innerHeight( )
Get the current computed height for the first element in the set of matched elements, including padding but not border.
int. innerWidth( )
Get the current computed width for the first element in the set of matched elements, including padding but not border.
int. outerHeight( [margin] )
Get the current computed height for the first element in the set of matched elements, including padding and border.
int. outerWidth( [margin] )
Get the current computed width for the first element in the set of matched elements, including padding and border.
Deferred
deferred object = {
defdone(doneCallbacks)
Add handlers to be called when the Deferred object is resolved.
deffail(failCallbacks)
A function, or array of functions, that are called when the Deferred is rejected.
boolisRejected()
Determine whether a Deferred object has been rejected.
boolisResolved()
Determine whether a Deferred object has been resolved.
defreject([args])
Reject a Deferred object and call any failCallbacks with the given args.
defrejectWith(context, [args])
Reject a Deferred object and call any failCallbacks with the given context and args.
defresolve([args])
Resolve a Deferred object and call any doneCallbacks with the given args.
defresolveWith(context, [args])
Resolve a Deferred object and call any doneCallbacks with the given context and args.
defthen(doneCallbacks, failCallbacks)
Add handlers to be called when the Deferred object is resolved or rejected.
}
def. promise(deferreds)1.6+
Return a Promise object to observe when all actions of a certain type bound to the collection, queued or not, have finished.
def. pipe(deferreds)1.6+
Utility method to filter and/or chain Deferreds.
def. always(deferreds)1.6+
Add handlers to be called when the Deferred object is either resolved or rejected.
|
Traversing
Filtering
$. eq( index )
Reduce the set of matched elements to the one at the specified index.
$. first( )
Reduce the set of matched elements to the first in the set.
$. last( )
Reduce the set of matched elements to the final one in the set.
$. has( selector ), . has( element )
Reduce the set of matched elements to those that have a descendant that matches the selector or DOM element.
$. filter( selector ), . filter( fn(index) )
Reduce the set of matched elements to those that match the selector or pass the function's test.
bool. is( selector | function(index) | jQuery object | element )1.6*
Check the current matched set of elements against a selector and return true if at least one of these elements matches the selector.
$. map( fn(index, element) )
Pass each element in the current matched set through a function, producing a new jQuery object containing the return values..
$. not( selector ), . not( elements ), . not( fn( index ) )
Remove elements from the set of matched elements.
$. slice( start [, end] )
Reduce the set of matched elements to a subset specified by a range of indices.
Tree traversal
$. children( [selector] )
Get the children of each element in the set of matched elements, optionally filtered by a selector.
$. closest( selector [, context] | jQuery object | element )1.6*
Get the first element that matches the selector, beginning at the current element and progressing up through the DOM tree.
arr. closest( selectors [, context] )
Gets an array of all the elements and selectors matched against the current element up through the DOM tree.
$. find( selector | jQuery object | element )1.6*
Get the descendants of each element in the current set of matched elements, filtered by a selector.
$. next( [selector] )
Get the immediately following sibling of each element in the set of matched elements, optionally filtered by a selector.
$. nextAll( [selector] )
Get all following siblings of each element in the set of matched elements, optionally filtered by a selector.
$. nextUntil( [selector] )
Get all following siblings of each element up to but not including the element matched by the selector.
$. offsetParent( )
Get all following siblings of each element up to but not including the element matched by the selector.
$. parent( [selector] )
Get the parent of each element in the current set of matched elements, optionally filtered by a selector.
$. parents( [selector] )
Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
$. parentsUntil( [selector] )
Get the ancestors of each element in the current set of matched elements, optionally filtered by a selector.
$. prev( [selector] )
Get the immediately preceding sibling of each element in the set of matched elements, optionally filtered by a selector.
$. prevAll( [selector] )
Get all preceding siblings of each element in the set of matched elements, optionally filtered by a selector.
$. prevUntil( [selector] )
Get all preceding siblings of each element up to but not including the element matched by the selector.
$. siblings( [selector] )
Get the siblings of each element in the set of matched elements, optionally filtered by a selector.
Miscellaneous
$. add( selector [, context] ), . add( elements ), . add( html )
Add elements to the set of matched elements.
$. andSelf( )
Add the previous set of elements on the stack to the current set.
$. contents( )
Get the children of each element in the set of matched elements, including text nodes.
$. end( )
End the most recent filtering operation in the current chain and return the set of matched elements to its previous state.
Manipulation
Inserting Inside
$. append( content ), . append( fn( index, html ) )
Insert content, specified by the parameter, to the end of each element in the set of matched elements.
$. appendTo( target )
Insert every element in the set of matched elements to the end of the target.
$. prepend( content ), . prepend( fn( index, html ) )
Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.
$. prependTo( target )
Insert every element in the set of matched elements to the beginning of the target.
Inserting Outside
$. after( content ), . after( fn() )
Insert content, specified by the parameter, after each element in the set of matched elements.
$. before( content ), . before( fn() )
Insert content, specified by the parameter, before each element in the set of matched elements.
$. insertAfter( target )
Insert every element in the set of matched elements after the target.
$. insertBefore( target )
Insert every element in the set of matched elements before the target.
Inserting Around
$. unwrap( )
Remove the parents of the set of matched elements from the DOM, leaving the matched elements in their place.
$. wrap( wrappingElement ), . wrap( fn )
Wrap an HTML structure around each element in the set of matched elements.
$. wrapAll(wrappingElement ), . wrapAll( fn )
Wrap an HTML structure around all elements in the set of matched elements.
$. wrapInner( wrappingElement ), . wrapInner( fn )
Wrap an HTML structure around the content of each element in the set of matched elements.
Replacing
$. replaceWith( content ), . replaceWith( fn )
Replace each element in the set of matched elements with the provided new content.
$. replaceAll( selector )
Replace each target element with the set of matched elements.
Removing
$. detach( [selector] )
Remove the set of matched elements from the DOM.
$. empty( )
Remove all child nodes of the set of matched elements from the DOM.
$. remove( [selector] )
Remove the set of matched elements from the DOM.
Copying
$. clone( [withDataAndEvents], [deepWithDataAndEvents] )
Create a copy of the set of matched elements.
|
Events
Page Load
$. ready( fn() )
Specify a function to execute when the DOM is fully loaded.
Event Handling
$. bind( type [, data ], fn(eventObj) )
Attach a handler to an event for the elements.
$. bind( type [, data], false )
Attach a handler to an event for the elements.
$. bind( array )
Attach a handler to an event for the elements.
$. unbind( [type] [, fn])
Remove a previously-attached event handler from the elements.
$. one( type [, data ], fn(eventObj) )
Attach a handler to an event for the elements. The handler is executed at most once.
$. trigger( event [, data])
Execute all handlers and behaviors attached to the matched elements for the given event type.
obj. triggerHandler( event [, data])
Execute all handlers attached to an element for an event.
$. delegate( selector, type, [data], handler)
Attach a handler to one or more events for all elements that match the selector, now or in the future, based on a specific set of root elements.
$. undelegate( [selector, type, [handler]]) | ( selector, events ) | ( namespace ) 1.6*
Remove a handler from the event for all elements which match the current selector, now or in the future, based upon a specific set of root elements.
Live Events
$. live( eventType [, data], fn() )
Attach a handler to the event for all elements which match the current selector, now or in the future.
$. die( ), . die( [eventType] [, fn() ])
Remove an event handler previously attached using .live() from the elements.
Interaction Helpers
$. hover( fnIn(eventObj), fnOut(eventObj))
Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
$. toggle( fn(eventObj), fn2(eventObj) [, ...])
Bind two or more handlers to the matched elements, to be executed on alternate clicks.
Event Helpers
function ( [data,] [fn] )
Event object
event = {
elcurrentTarget,
The current DOM element within the event bubbling phase.
*data,
Contains the optional data passed to jQuery.fn.bind when the current executing handler was bound.
boolisDefaultPrevented(),
Returns whether event.preventDefault() was ever called on this event object.
boolisPropagationStopped(),
Returns whether event.stopPropagation() was ever called on this event object.
strnamespace,
This attribute contains the namespace specified when the event was triggered.
numpageX,
The mouse position relative to the left edge of the document.
numpageY,
The mouse position relative to the top edge of the document.
preventDefault(),
If this method is called, the default action of the event will not be triggered.
elrelatedTarget,
The other DOM element involved in the event, if any.
objresult,
This attribute contains the last value returned by an event handler that was triggered by this event, unless the value was undefined.
stopPropagation(),
This method prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event.
eltarget,
The DOM element that initiated the event.
numtimeStamp,
This attribute returns the number of milliseconds since January 1, 1970, when the event is triggered.
strtype,
Describes the nature of the event.
strwhich
For key or button events, this attribute indicates the specific button or key that was pressed.
}
Effects
Basics
$. show( [ duration [, easing] [, fn] ] )
Display the matched elements.
$. hide( [ duration [, easing] [, fn] ] )
Hide the matched elements.
$. toggle( [showOrHide] )
Bind two or more handlers to the matched elements, to be executed on alternate clicks.
$. toggle( duration [, easing] [, fn] )
Bind two or more handlers to the matched elements, to be executed on alternate clicks.
Sliding
$. slideDown( duration [, easing] [, fn] )
Display the matched elements with a sliding motion.
$. slideUp( duration [, easing] [, fn] )
Hide the matched elements with a sliding motion.
$. slideToggle( [duration] [, easing] [, fn] )
Display or hide the matched elements with a sliding motion.
Fading
$. fadeIn( duration [, easing] [, fn] )
Display the matched elements by fading them to opaque.
$. fadeOut( duration [, easing] [, fn] )
Hide the matched elements by fading them to transparent.
$. fadeTo( [duration,] opacity [, easing] [, fn] )
Adjust the opacity of the matched elements.
$. fadeToggle( [duration,] [, easing] [, fn] )
Display or hide the matched elements by animating their opacity.
Custom
$. animate( params [, duration] [, easing] [, fn] )
Perform a custom animation of a set of CSS properties.
$. animate( params, options )
Perform a custom animation of a set of CSS properties.
$. stop( [clearQueue] [, jumpToEnd] )
Stop the currently-running animation on the matched elements.
$. delay( duration [, queueName] )
Set a timer to delay execution of subsequent items in the queue.
|
AJAX
Low-Level Interface
jqXHRjQuery.ajax( options, [settings] )
- mapaccepts
The content type sent in the request header that tells the server what kind of response it will accept in return.
- boolasync = true
By default, all requests are sent asynchronous.
- fnbeforeSend( jqXHR, config)
A pre-request callback function that can be used to modify the jqXHR object before it is sent.
- boolcache = true
If set to false it will force the pages that you request to not be cached by the browser.
- fncomplete( jqXHR, status)
A function to be called when the request finishes (after success and error callbacks are executed).
- mapcontents
A map of string/regular-expression pairs that determine how jQuery will parse the response, given its content type.
- strcontentType
When sending data to the server, use this content-type. Default is "application/x-www-form-urlencoded", which is fine for most cases.
- objcontext
This object will be made the context of all Ajax-related callbacks.
- mapconverters
A map of dataType-to-dataType converters. Each converter's value is a function that returns the transformed value of the response.
- boolcrossDomain
If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true.
- obj, strdata
Data to be sent to the server. It is converted to a query string, if not already a string.
- fndataFilter( data, type )
A function to be used to handle the raw responsed data of XMLHttpRequest.This is a pre-filtering function to sanitize the response.
- boolglobal = true
Whether to trigger global Ajax event handlers for this request.
- mapheaders
A map of additional header key/value pairs to send along with the request.
- boolifModified = false
Allow the request to be successful only if the response has changed since the last request.
- strjsonp
Override the callback function name in a jsonp request.
- fnjsonpCallback
Specify the callback function name for a jsonp request.
- strpassword
A password to be used in response to an HTTP access authentication request.
- boolprocessData = true
By default, data passed in to the data option as an object will be processed and transformed into a query string, fitting to the default content-type "application/x-www-form-urlencoded".
- strscriptCharset
Forces the request to be interpreted as a certain charset.
- mapstatusCode
A map of numeric HTTP codes and functions to be called when the response has the corresponding code.
- numtimeout
Set a local timeout (in milliseconds) for the request.
- booltraditional
Set this to true if you wish to use the traditional style of param serialization.
- strtype = 'GET'
The type of request to make ("POST" or "GET"), default is "GET".
- strurl = curr. page
A string containing the URL to which the request is sent.
- strusername
A username to be used in response to an HTTP access authentication request.
- fnxhr
Callback for creating the XMLHttpRequest object.
- strdataType ∈ {xml, json, script, html}
The type of data that you're expecting back from the server.
- fnerror( jqXHR, status, errorThrown )
A function to be called if the request fails.
- fnsuccess( data, status, jqXHR )
A function to be called if the request succeeds.
Shorthand Methods
$. load( url [, data] [, fn( responseText, status, XHR )] )
Load data from the server and place the returned HTML into the matched element.
jqXHRjQuery.get( url [, data] [, fn( data, status, XHR )] [, type] )
Load data from the server using a HTTP GET request.
jqXHRjQuery.getJSON( url [, data] [, fn( data, status )] )
Load JSON-encoded data from the server using a GET HTTP request.
jqXHRjQuery.getScript( url [, fn( data, status )] )
Load a JavaScript file from the server using a GET HTTP request, then execute it.
jqXHRjQuery.post( url [, data] [, fn( data, status )] [, type] )
Load data from the server using a HTTP POST request.
Global Ajax Event Handlers
$. ajaxComplete( fn( event, XHR, options ) )
Register a handler to be called when Ajax requests complete.
$. ajaxError( fn( event, XHR, options, thrownError ) )
Register a handler to be called when Ajax requests complete with an error.
$. ajaxSend( fn( event, XHR, options ) )
Show a message before an Ajax request is sent.
$. ajaxStart( fn( ) )
Register a handler to be called when the first Ajax request begins.
$. ajaxStop( fn( ) )
Hide a loading message after all the Ajax requests have stopped.
$. ajaxSuccess( fn(event, XHR, options) )
Show a message when an Ajax request completes successfully.
Miscellaneous
str. serialize( )
Encode a set of form elements as a string for submission.
[obj]. serializeArray( )
Encode a set of form elements as an array of names and values.
strjQuery.param( obj, [traditional] )
Create a serialized representation of an array or object, suitable for use in a URL query string or Ajax request.
Utilities
Browser and Feature Detection
objjQuery.support
A collection of properties that represent the presence of different browser features or bugs.
objjQuery.browserdeprecated
Contains flags for the useragent, read from navigator.userAgent.
booljQuery.boxModeldeprecated
States if the current page, in the user's browser, is being rendered using the W3C CSS Box Model.
Basic operations
objjQuery.each( obj, fn( index, valueOfElement ) )
Iterates through the array displaying each number as both a word and numeral.
objjQuery.extend( [deep,] target, obj1 [, objN] )
Merge the contents of two or more objects together into the first object.
arrjQuery.grep( array, fn( element, index ) [, invert] )
Finds the elements of an array which satisfy a filter function. The original array is not affected.
arrjQuery.makeArray( obj )
Convert an array-like object into a true JavaScript array.
arrjQuery.map( arrayOrObject, fn( element, index ) )1.6*
Translate all items in an array or array-like object to another array of items.
numjQuery.inArray( val, array )
Search for a specified value within an array and return its index (or -1 if not found).
arrjQuery.merge( first, second )
Merge the contents of two arrays together into the first array.
fnjQuery.proxy( fn, scope ), jQuery.proxy( scope, name )
Takes a function and returns a new one that will always have a particular context.
arrjQuery.unique( array )
Sorts an array of DOM elements, in place, with the duplicates removed. Note that this only works on arrays of DOM elements, not strings or numbers.
strjQuery.trim( str )
Remove the whitespace from the beginning and end of a string.
objjQuery.parseJSON( str )
Takes a well-formed JSON string and returns the resulting JavaScript object.
Data functions
$. clearQueue( [name] )
Remove from the queue all items that have not yet been run.
$. dequeue( [name] ), jQuery.dequeue( [name] )
Execute the next function on the queue for the matched elements.
obj. data( ), . data( key )
Store arbitrary data associated with the specified element.
$. data( key, val ), . data( obj )
Store arbitrary data associated with the specified element.
$. removeData( [name] )
Remove a previously-stored piece of data.
[fn]. queue( [name] ) jQuery.queue( [name] )
Show the queue of functions to be executed on the matched elements.
$. queue( [name,] fn( next ) ), jQuery.queue( [name,] fn( ) )
Show the queue of functions to be executed on the matched elements.
$. queue( [name,] queue ), jQuery.queue( [name,] queue )
Show the queue of functions to be executed on the matched elements.
Test operations
strjQuery.type( obj )
Determine the internal JavaScript [[Class]] of an object.
booljQuery.isFunction( obj )
Determine if the argument passed is a Javascript function object.
booljQuery.isPlainObject( obj )
Check to see if an object is a plain object (created using "{}" or "new Object").
|